CS173: Intro to Computer Science - Tic-Tac-Toe (3 Points)
Developed by Professor Tralie and Professor Mongan.Exercise Goals
The goals of this exercise are:- To iterate over a 2-dimensional array
Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is wmongan
(non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit)
Netid |
TicTacToe.java
Driver.java
Output
In this exercise [1], students will write a program that simulates a game of Tic-Tac-Toe. The Tic-Tac-Toe game is played on a 3x3 grid with two players who take turns. The first player marks moves with an X, and the second player marks moves with an O. The first player to form a horizontal, vertical, or diagonal sequence the same mark wins the game. In a 3x3 board, this is 3 such marks in a row. Your program should check that a given board configuration is a winning board.
Note that using this interface, it is necessary to compare char variables with double quotes instead of single quotes (even though you will use single quotes in real Java code). That is, to check if a variable ch
is equal to the letter “X”, you would write:
if(ch == "X") {
// code to execute if true
}
You should use a loop in your submission. To check your iteration, a test case is included that checks a 4x4 board for tic-tac-toe using the same rules as the standard 3x3 board (that is, that the whole column, row, or diagonal is filled with the same player’s mark). In other words, you are now searching for 4 marks in a row, but should use the same logic as you had written when using the 3x3 board.
One way to go about this is to count how many X’s and O’s you find in a given row, column, or diagonal (three separate loops). If the total number of X’s or O’s is equal to the length of that row, column, or diagonal, you can return true
. If, after all of these checks, you haven’t found one of these winning conditions, return false
.
-
Developed by Prof. Chris Tralie ↩