728x90 AdSpace

Latest Article



tic-tac-toe game Practice Question for C++ and Java Example





tic-tac-toe game Practice Question for C++ and Java Example  For Exam and Assignment 

You will create a program to read the state of a tic-tac-toe game board from a file and determine whether there is a winner or not. Tic-tac-toe is a game played on a 3x3 grid. There are two players X and O (it's the letter O, not the number 0) each of which take turns putting their symbol in one of the cells. The first player to fill a column, row or diagonal with their symbol wins the game. The input file can contain multiple games for which you have to find winners. You will write the results to a file called “results.txt”. If there is a winner, say O, you output “O wins”. If there is no winner, you will simple output “Draw”.

When testing the 3x3 game board for a win, you can theoretically test the rows and column in any order. But practically, we are going to tell you how to do it. The order you need to follow is,
-         Check rows for a win, starting from the top row
-         Check columns for a win, starting from the left-most column
-         Check the major diagonal for a win
-         Check the minor diagonal for a win

The format of the input file is very simple. The first 3 lines will contain the first game board state, followed by a blank line and another game board state and so on an so forth. Basically, each game board state is separated by a blank line. Your program will stop taking the input if you find the letter 'q' instead of an empty line after reading a game board state. You can assume that the input file will have no errors in it. Suppose you are given an input file containing the following configurations.

XXX
OXO
OOX

OXO
OXX
OOX
q
OOX
XXO
OXO
Your program should write the following output to results.txt.

X wins
O wins

Notice that the program did not give any output for the third game board state in the input file. This is because after reading the second game board state, the program finds a 'q' instead of a blank line and this causes it to terminate. If there was a 'q' after the third state, the output would be,

X wins
O wins
Draw

A sample input file will be provided to you. If you don't have it, please ask for it. 

no image
  • Title : tic-tac-toe game Practice Question for C++ and Java Example
  • Posted by :
  • Date : 04:18
  • Labels :






  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment