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:
  1. To iterate over a 2-dimensional array
Modify the TicTacToe.java file to return whether a given array contains a winning tic-tac-toe configuration.

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
Clicking Run below will check your work and, if it passes, will submit your work automatically. You must be connected to the VPN for submission to be successful! You will receive a copy of your code via e-mail, so you'll know that it was submitted if you receive that e-mail! VPN access requires Multi-Factor Authentication, which sends you a code when you log into the network. Instructions on configuring these for your account can be found here.

TicTacToe.java

public class TicTacToe { public static boolean checkWinningTicTacToe(char[][] board) { // return true if a winning tic-tac-toe board is given // for example, if the first element in any row is equal to the second element in that row, and also equal to the third element in that row. // ... the same logic is applied to every column, and to both diagonals of the board. // Use a loop to check each element of a row/column/diagonal. The .length property of an array variable will be helpful in determining the bounds of (when to stop) the loop } }

Driver.java

public class Driver { public static void main(String[] args) { char[][] board1 = new char[][] { { "X", "O", "X" }, { "O", "X", "-" }, { "-", "-", "X" } }; char[][] board2 = new char[][] { { "X", "O", "X", "-" }, { "O", "O", "X", "-" }, { "-", "-", "X", "-" }, { "-", "-", "X", "O" } }; boolean isWinner1 = TicTacToe.checkWinningTicTacToe(board1); boolean isWinner2 = TicTacToe.checkWinningTicTacToe(board2); boolean bothWinners = isWinner1 && isWinner2; System.out.println(bothWinners); } }
Driver.main(null);

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.

  1. Developed by Prof. Chris Tralie