We can use a for loop to iterate through each row i. In tic tac toe, a player wins if they have 3 of their symbols in one row, column, or diagonal. Step 8: Create a function that checks if either player has won. Now we can set this position to be equal to the char of the player, which we stored in the variable c. We can get the position on the board by doing board. Outside of the while loop, we know that we have a valid row and col. Step 7: Set the right position on the board to the player char. Hint: We can use a while(true) loop and break once the player has entered a valid row and col. If the user enters a row and col that is out of bounds or a row and col that already has an x or o on it, then we want to ask the user to re-enter a row and col. Step 6: Use a loop to keep asking the player to enter a valid row and col. Use a conditional to check if the position on the board at row and col does not already have an x or o. Hint: If the user types a row and col that is a spot that already has an x or o on it, then the row and col aren’t valid. Use a conditional to check if the row and col are not greater than 2 and not less than 0. Hint: If the user types a row and col that is a spot that is not on the board, then the row and col aren’t valid. Now, why would the row and col the user entered not be valid? Print a message asking the user for a row and use the Scanner to get their input, storing it in a variable called row repeat this for col. Step 5: Ask the user for the row and col and check if it is valid. We can use a conditional to check whose turn it is.Īlso, we can use string concatenation to print out the player’s name. Hint: We can use a boolean called player1 which is true if it is player 1’s turn and false if it is player 2’s turn. We need a way to keep track of which player’s turn it is in our game. Step 4: Print out the correct player’s turn and store the player’s char (x or o). Hint: We can do () in the inner for loop, and do () at the end of the outer for loop so that it starts a new line after each row has been printed. Hint: If we do (), then all of the positions are on one line. Hint: If we do (), then each position is on a new line. Inside our function, we need to print out each position on our board. We don’t need to return anything since the function is simply printing out the board. Hint: We need to pass in the board 2D array in order for the function to be able to print it. In order for our function to draw the board and print it out, do we need to pass a parameter into the function? Do we need to return anything? Step 3: Create a function that draws the board and prints it out like a 3×3 square. We store their input in a String called p1.ĭo the same for p2. Next, we print out a message asking the user to type in their name using (). Step 2: Ask the users for their names.įirst, we import the Scanner to help us get input from the user, by adding import to the top of our program. Inside both for loops, we can set board equal to a dash. Hint: We can use a nested for loop to iterate through each position on our board. Now we have to fill our board with dashes. Hint: We can use the following line of code to make a 3×3 array of chars: char board = new char We need to make a 2D array of characters, which can be x, o, or. How do we do each of these steps? Step 1: Create a 3×3 array to represent the tic tac toe board and fill it with dashes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |