i would like to code a single player game where at each turn a player has to write a letter on a grid. In response the computer would write a letter and so on. As soon as a word is created as a result of writing a letter, the player gets as many points as the no of letters in the word. how would i code it.
That’s a pretty easy almost-infinite loop. The main loop is something very common in game programming.
In Pseudo code:
playerTurn = 1 // playerTurn = 1 means its players turn, = 0 means computers turn somebodyWon = False while somebodyWon = False: if playerTurn: askPlayerForInput() else: writeInputFromComputerArtificialIntelligence() somebodyWon = checkIfSomebodyHasWonByUsingADictionary()
As you see, you need several components here:
- Main class where you handle this loop
- Some input for the player to input a letter (e.g.
scanf()in C or
- Some easy artificial intelligence for the computer (he could search for words in his dictionary and add one letter for some real word he sees in the dictionary). Ideally, the computer should only know a subset of all real words. This could depend on the difficulty of your game (e.g. on very easy it knows 10% of all words, on very hard it knows 90% of all words)
- A check for the input word against a list of winner words: Just take the word player + computer have already created and search in a dictionary (list of all real words) for it. If it is there, somebody has won.