Tic-Tac-Toe Game w/ 2D arrays Help!

This the question:


Two Dimensional Arrays (courtesy of Dr. Huo)
The following program is the base for the game: tic-tac-toe. The main function and program structure is given, don’t modify them. Complete three functions as required. Compile and run this program, and then you can play the game. Have fun!

#include<iostream>
using namespace std;

const int DIM=3;
char chessboard[DIM][DIM];
//initChessBoard
void initChessBoard(char cb[][DIM])
{
//set all the elements of the ChessBoard to blanks
//Complete this part in the following:

}

//printChessBoard
void printChessBoard(char cb[][DIM])
{
//print all the elements of the chessBoard with each row in one line
//Complete this part in the following:

}

//putChequer
bool putChequer(char cb[][DIM], int i, int j, char x)
{
/* if i and j are not out of bound(that is, i and j are in the range of 0 and DIM-1) and cb[i][j] is not occupied(that is, the value of cb[i][j] is blank), set cb[i][j] to be the value of x and return true. Otherwise, return false.*/
// Complete this part in the following:

}
//judge the state of the game. The player has put x in the position (row, col).
// If all the elements in this row are x, x wins
// If all the elements in this column are x, x wins.
// If x is in the main diagonal, and if all the elements in the main diagonal are x, x wins.
// If x is in the opposite diagonal, and if all the elements in the opposite diagonal are x, x wins.

bool state(char cb[][DIM], int row, int col, char x)
{

/* We declare four variables count1, count2, count3, count4 to represent the occurrence number of x in row number row, column number col, in the main diagonal, and in the opposite diagonal. If after calculation, count1, count2, count3 or count4 equals to DIM, return true(that is, x wins). */
int count1=0, count2=0, count3=0, count4=0;

for(int i=0; i<DIM; ++i)
{
// Complete this part in the following:
// if the element in position (row, i) is x, count1 is increased by 1.

// if the element in position (i, col) is x, count2 is increased by 1.

/* if x is in the main diagonal, and the element in position (i, i) is x, count3 is increased by 1.*/

/* if x is in the opposite diagonal, and the element in position (i, DIM-1-I) is x, count4 is increased by 1.*/



}
return (count1==DIM || count2==DIM || count3==DIM || count4==DIM);
}


int main()
{
int row, col;
int blanks=DIM*DIM;
initChessBoard(chessboard);
printChessBoard(chessboard);
char cur='O';

cout<<"Input the position(row col), (-1 -1) for exit; It is the turn of "<<cur<<endl;
cin>>row>>col;

while(row!=-1 && col!=-1)
{
if(!putChequer(chessboard, row, col, cur))
{
cout<<"Invalid move"<<endl;
printChessBoard(chessboard);
}
else
{
--blanks;
printChessBoard(chessboard);
if(state(chessboard, row, col, cur))
{
cout<< cur << " Wins"<<endl;
return 0;
};
if(blanks==0)
{
cout<< "Ties"<<endl;
return 0;
}
if(cur=='X')
cur= 'O';
else cur='X';
}
cout<<"Input the position(row col), (-1 -1) for exit; It is the ture of "<<cur<<endl;
cin>>row>>col;
}
}


This is what I did so far:

I need help on what to do next.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;
const int DIM=3;
char chessboard[DIM][DIM];
void initChessBoard(char cb[][DIM]);
void printChessBoard(char cb[][DIM]);
bool putChequer(char cb[][DIM], int i, int j, char x);
bool state(char cb[][DIM], int row, int col, char x);
int main()
{

	int row, col;
	int blanks=DIM*DIM;
	initChessBoard(chessboard);
	printChessBoard(chessboard);
	char cur='O';

	cout<<"Input the position(row col), (-1 -1) for exit; It is the turn of "<<cur<<endl;
	cin>>row>>col;	

	while(row!=-1 && col!=-1)
	{
		if(!putChequer(chessboard, row, col, cur))
		{
			cout<<"Invalid move"<<endl;
			printChessBoard(chessboard);
		}
		else
		{
			--blanks;
			printChessBoard(chessboard);
			if(state(chessboard, row, col, cur))
			{
				cout<< cur << " Wins"<<endl;
				return 0;
			};
			if(blanks==0)
			{
				cout<< "Ties"<<endl;
				return 0;
			}
			if(cur=='X') 
				cur= 'O';
			else cur='X';
		}
		cout<<"Input the position(row col), (-1 -1) for exit; It is the ture of "<<cur<<endl;
		cin>>row>>col;
	}
}

void initChessBoard(char cb[][DIM])
{
	for (int x = 0; x < 3; x++)
	{
		for (int y = 0; y < 3; y++)
		{
			cout<<cb[x][y];
			cout<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
}
void printChessBoard(char cb[][DIM])
{
	cout<<"** Tic Tac Toe **\n\n";

	for (int x = 0; x < 3; x++)
	{
		for (int y = 0; y < 3; y++)
		{
			cout<<cb[x][y];
			cout<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
}
bool putChequer(char cb[][DIM], int i, int j, char x)
{

}
bool state(char cb[][DIM], int row, int col, char x)
{
	int count1=0, count2=0, count3=0, count4=0;

	for(int i=0; i<DIM; ++i)
	{

	}

	return (count1==DIM || count2==DIM || count3==DIM || count4==DIM);
}
Last edited on
Alright 1st you Really need to Comment your work! Always good practice .. although I'm also still new to C++ and Learning, It makes it so much easier to read your own work and for other to easily help you out!

I would take a step back and write it all out in Pseudocode..
so you can see what you have to do.. and take it step by step..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Create an empty tic-tac-toe bored
Display the game instructions
Determine who goes 1st
Display the board
While nobody has won && is NOT a tie
     If its the humans turn
          Get the humans move
          Update the board with the humans move
     Otherwise
          Calculate the computers move
          Update the board with the computers move
     Display the board
     Switch turns
Congratulate the winner or declare a tie
Last edited on
Topic archived. No new replies allowed.