THINK FIRST·CODE LATER

← All labs

Tic-tac-toe referee

Problem

Write TicTacToe. Read a 3 × 3 board as three words of three characters (X, O or . for empty) into a char[][]. Write the methods wins(char[][] board, char player) (a full row, column or diagonal) and count(char[][] board, char player).

Print the counts, then one verdict: X wins, O wins, Draw (board full, no winner), Game in progress, X to move (X plays first, so X moves when the counts are equal), or Impossible position (O has more marks than X, X has more than one extra mark, or both players have a winning line).

For the input

XOX
OXO
OXX

the program prints

X: 5, O: 4, empty: 0
X wins

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l9-6-data-1.txt

XOX
OXO
OXX
What your program must print for l9-6-data-1.txt
X: 5, O: 4, empty: 0
X wins

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l9-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l9-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.