-
Tic Tac Toe problem
I need to write a program that tells the state of a Tic-Tac-Toe game, based off a board input by the user. The program needs to return a single character: X if x wins, O if o wins, or D if it is a draw, and * if none of those conditions exist. I'm supposed to write a function to do this, and call it in the main. I'm a bit stuck on how to go about doing this. So far all I have is a main function that prints the board after inputted by the user. Suggestions? Here's my code:
Code:
#include <iostream>
using namespace std;
int main()
{
char board[3][3];
int i=0, j=0;
cout << "Enter the board" << endl;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin >> board[i][j];
}
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout << board[i][j];
}
cout << endl;
}
return 0;
}
-
Well perhaps something like
Code:
char checkBoard ( char board[3][3] ) {
char winChar = '*';
// your code to calculate 'X' 'O' or 'D'
return winChar;
}
Your main then contains
cout << checkBoard(board) << endl;
-
Thank you. In addition though, I'm given a hint to use a for loop that compares each element of the diagonal with the elements of it's intersecting row and column, and then check diagonals. Unfortunately, the book I'm using doesn't really give much explanation, nor examples of how to actually do this. How does one exactly make that for loop?
-
Indent your statements by one level within curly braces.
IMHO The thing is with stuff like this is there is a certain amount of 'extrapolation' expected..methinks. I.E. if you understand that [3][3] represents a 3 by 3 square board, then it follows that [0][0] is top left corner [1][1] is centre etc, this then lets you manipulate the variables you use in the index to check for diagonal matches or whatever.
Your board setup uses a nested for loop, you could use this idea to achieve your goal perhaps.
Salem has shown a char checkBoard(..) function, you could take this idea and add an additional parameter, to check status of player X then O and allow your return to also be shown as D if that is found to be the outcome, the additional parameter is not really neccesary because logically you can gather the result in one call, but maybe play with it.
And in a given board postion there is the always the possibility that play can continue, no win, no draw, so that must be accomodated too. Unless you only have one known outcome to test as part of your excercise of course, why work harder than neccesary ;->
-
Thank you for the thoughts! But one last question, since my book hardly goes into any detail about 2 dimensional Arrays, I don't really know how to start the code to compare, say, the top left corner of the board to, say, the square to the right of it, or the square in the middle, etc. I know that writing [0][0] would be the top left corner, [0][1] would be one to the right, but how do I incorporate that into code? Lets just say I call my Array board[i][j], how would I start off with the top left corner, and compare it to the square next to it? and the one below it? Once I figure out how to do that, I think I have a good idea how to write the function.
-
you have already used this:
Code:
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin >> board[i][j];
}
}
[i] stays at 0 until [j] exits loop, i goes to 1 until j exits loop..etc.
each time between the changes for [i] ; j is going 0 , 1 , 2 <3=break>
so that is a way to step through all the board positions.
so you might want to treat each [i][j] as the 'current' postion, and then for example test the top left position relative to the current i,j : ask: ¿what is contained in [i-1][j-1] compared to what is in [i][j] ? But you need to rememeber you cant try and look beyond bounds eg [-1][0] or [1][4] so your code has to protect against that