Tic Tac Toe Game! Day 8
Game Description We will be working on an implementation of a Tic-Tac-Toe Game. This is designed as a two-player game. As you get more involved in programming, you might learn how to design one with the computer using random numbers :) For this game, there will be two players one represented as x and one as o. The first turn is Player X s turn. Each player can place one piece on the board during their turn. The player decides where to place their piece by inputting a row and column coordinate. A player wins when 3 of their pieces form a line; diagonal, horizontal, and vertical all count.
Example Run:
Creating the tic-tac-toe board (part 1) In order to create the tic-tac-toe board, we store it as a string. But first let s write the board as a series of strings! The first row of the board as a string would be: string row1 = 1 2 3\n ; index counting: string row2 = 1 \n ; index counting: 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 12 string row3 = - - - - -\n ; index counting: 0 1 2 3 4 5 6 7 8 9 10 11 // what are the last 3 rows? // remember that \n is ONE character
Creating the tic-tac-toe board (part 2) Once you have all the rows stored in strings, we want to put them into one string. Remember from earlier: string str1 = pan ; string str2 = cake ; string str3 = str1 + str2; // what does str3 store? So if we currently have string row1 = 1 2 3\n ; string row2 = 1 \n ; string row3 = - - - - -\n ; etcetera, how would we put them into one string that stores the whole board? string board = ;
printboard This function should take in a string that represents the entire board. Then it should output the string to the console. HINT: do not return the string! For aesthetic purposes, maybe cout newlines before and after the board so it is separated a bit from the other outputs.
String [] The string class has the bracket function. It can take in an index position inside the [] and will return the character at that position. This function is unique because it allows you to change the value of the char inside the string. Ex. // These do the same thing string word = California Adventure ; char letter1 = word.substr(1,1); char letter2 = word[1]; //But, we can edit chars in the string! word[1] = A ; cout << word; //What is the output?
getindex This function should accept two int parameters, a row position and a column position. It should return the index in the giant string board where that coordinate is located at. Ex: getindex(1,1); string row1&2 = 1 2 3\n1 \n ; If I want to change the value in the string, I would have to know the index. Which is why we need this function.
Indexing board: Remember! The entire string board written out would look like: string board = " 1 2 3\n1 \n - - - - -\n2 \n - - - - -\n3 \n"; If we wanted the index of position (1,1), the function getindex would return 14. If we wanted the index of position (1,2), the function getindex would return 18. If we wanted the index of position (1,3), the function getindex would return 22. 1 2 3\n1 \n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
getindex int getindex(int row, int col){ if(row == 1 && col == 1){ return 14; } else if( row == 1 && col == 2){ return 18; } else if( row == 1 && col == 3){ return 22; } // what is the rest of the code? }
Passing by Value Let s create a function that adds ten to the number inputted into the function //Outside of main int add_10(int val){ val += 10; return val; } //Using in main int main(){ int numb = 20; add_10(numb); cout << numb; return 0; } We had explained earlier that a variable passed into the functions would be only take the value of it. This line is taking the value of 10 and saving 10 into val. When this function is returned, this line will have a value of 20. But has numb changed? What if we want numb to change?
Passing by Reference When declaring a function, we can pass in a variable by reference which means it will be able to update changes to the variable passed in a function. If you want to pass in a variable by reference, you will include an & before the name of the variable in the function parameter list. Ex: int add_10(int &val){ } val += 10; return val; Can only use this with variables passed in! int main(){ int numb = 10; add_10(numb); add_10(numb); add_10(numb); add_10(10); //ERROR! DO NOT DO PLz } cout << numb; //what is the output
Visual Representation
mark : Place pieces on board This function will place an x or o in a position, given as row and column, on the board. What information (parameters) do we need to make this function? What data types are the parameters? What would the return type be?
mark So mark needs : Row number where the piece will be placed (int) Column number where the piece will be placed (int) Entire board (string) Which piece it is, either x or o (char) Should any be passed by reference?
win: checking to see if someone won Write a function called win which checks to see if someone won! You decide on which parameters the function should have. We recommend that function returns a bool (true if someone won and false if someone has not won). Remember, there are 8 ways to win tic tac toe!
win General idea: we want to see if there are three of the same mark in a row. Example: //checking to see if there are three x s in the first row if (board[14] == x && board[18] == x && board[22] == x ) { return true; } //what are the other 7 ways for x to win? What about o winning? Note: we must use the single quotes when writing x because the viewing brackets look at a char within the string at a particular index.
Optional Functions printboard, getindex, mark, and win are the four functions that are necessary for the tic tac toe game to work. However, you can also write other functions that may be helpful in your code! One suggestion is making a winner function, which will cout a message depending on who won (or if no one won). If you have any other function in mind that you want to code, let one of us know and we can help you construct it!
Writing the tic tac toe program Most games that we know are repetitive in some way or another. Tic Tac Toe is not any different. Therefore, it would be a good idea to use a loop somewhere in our game. Think about it... What parts of the game are repeated? What is the most number of times this process is repeated? What is the least number of times this process is repeated? When should the repeated process stop? (What are the conditions?) What would be the best loop to use?
Alternating Turns Within the loop, Player X and Player O alternate turns. This means that: if (it is Player X s turn), certain code should be executed, or else it is Player O x turn and certain code should be executed. There are several different ways that we can write the conditions for this code. Please share with us your ideas on how we could do this!
Player X s Turn First the computer will tell them it s their turn as well as asking them where would they like to move. Then the player must enter the row and column of where they would like to move. The numbers should be separated by a space. Lastly the board is printed with their move. How would we code this? Remember! Use your functions.
Player O s Turn Player O s turn is very similar to Player X s turn! First the computer will tell them it s their turn as well as asking them where would they like to move. Then the player must enter the row and column of where they would like to move. The numbers should be separated by a space. Lastly the board is printed with their move. How would we code this? Remember! Use your functions.
Concluding the game After the game, if you want to print a message based off of who won, or a tie, this code or (function) would be used after the loop! If you do not wish to include this part, that s fine. Congratulations! You have created a tic-tac-toe game! We thank you for sticking around the entire two weeks (: