Tic Tac Toe, Comments/Optimization/etc...

I wrote a simple console game for tic tac toe. I wanted to put it up here in hopes that someone could look it over and give me some feedback (on my implementation/coding style/etc.) Also, I have only written in java so im not used to passing by reference for parameters in functions, what I was wondering is... should I have passed the array to each function by reference instead of how i have it implemented here? Im not sure if the way i am doing it, causes the array to be copied every time it is being called? if it is, then calling it by reference would cause the program to be much more efficient, correct? (not that it would really matter with a program this size, but i gotta start thinking about things like this somewhere right?) Thanks in advance. and here is the code:

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>

using namespace std;

void printBoard(char[3][3], int, int);
int getRow(void);
int getCol(void);
void playerOMove(char array[][3]);
void playerXMove(char array[][3]);
bool checkOWin(char array[][3]);
bool checkXWin(char array[][3]);

int main ()
{	
	// Named constants for array dimensions
	const int ROWS = 3;
	const int COLUMNS = 3;
	
	// Initialize Board
	char board[ROWS][COLUMNS] = {{'-','-','-'},
		{'-','-','-'},
		{'-','-','-'}};
	
	bool isWinner = false;
	int counter = 0;
	printBoard(board, ROWS, COLUMNS);
	
	do {
		
		// Allow player X to move, check if X wins, and print the board
		playerXMove(board);
		isWinner = checkXWin(board);
		// If X wins, print the board and exit loop
		if (isWinner) { printBoard(board, ROWS, COLUMNS); break; }
		printBoard(board, ROWS, COLUMNS);
		
		// only player X gets to move on last iteration. board should then be full
		if (counter < 4) {
			// Allow player O to move, check if O wins, and print the board
			playerOMove(board);
			isWinner = checkOWin(board);
			// If O wins, print the board and exit loop
			if (isWinner) { printBoard(board, ROWS, COLUMNS); break; }
			printBoard(board, ROWS, COLUMNS);
		}
		
		counter++;
	} while (counter < 5);
	
	if (!isWinner) cout << "Tie! No Winner, Great Game!" << endl;
	
	return 0;
}

bool checkXWin(char array[][3])
{
	bool winner = false;
	if ( array[0][0] + array[0][1] + array[0][2] == 264
			|| array[1][0] + array[1][1] + array[1][2] == 264
			|| array[2][0] + array[2][1] + array[2][2] == 264
			|| array[0][0] + array[1][0] + array[2][0] == 264
			|| array[0][1] + array[1][1] + array[2][1] == 264
			|| array[0][2] + array[1][2] + array[2][2] == 264
			|| array[0][0] + array[1][1] + array[2][2] == 264
			|| array[0][2] + array[1][1] + array[2][0] == 264 )
		winner = true;
	if (winner) cout << "Player X Wins!\nGreat Job!" << endl;
	return winner;
}

bool checkOWin(char array[][3])
{
	bool winner = false;
	if ( array[0][0] + array[0][1] + array[0][2] == 237
			|| array[1][0] + array[1][1] + array[1][2] == 237
			|| array[2][0] + array[2][1] + array[2][2] == 237
			|| array[0][0] + array[1][0] + array[2][0] == 237
			|| array[0][1] + array[1][1] + array[2][1] == 237
			|| array[0][2] + array[1][2] + array[2][2] == 237
			|| array[0][0] + array[1][1] + array[2][2] == 237
			|| array[0][2] + array[1][1] + array[2][0] == 237 )
		winner = true;
	if (winner) cout << "Player O Wins!\nGreat Job!" << endl;
	return winner;
}

// Asks user to input what row for move
int getRow(void)
{
	int row;
	
	do {
		cout << "What row to make move?" << endl;
		cin >> row;
		// check for valid input
		if (row > 2 || row < 0) cout << "Incorrect row, please choose between 0, 1, or 2." << endl;
	} while (row > 2 || row < 0);
	
	return row;
}

// Asks user to input what column for move
int getCol(void)
{
	int col;
	
	do {
		cout << "What column to make move?" << endl;
		cin >> col;
		// check for valid input
		if (col > 2 || col < 0) cout << "Incorrect column, please choose between 0, 1, or 2." << endl;
	} while (col > 2 || col < 0);
	
	return col;
}

// Allows Player 'X' to move
void playerXMove(char array[][3])
{
	while (true) {
		cout << "Pleyer X's turn" << endl;
		int row = getRow();
		int col = getCol();
		// check if space is taken
		if (array[row][col] == '-') { array[row][col] = 'X'; break; }
		cout << "Space already occupied, please select another." << endl;
	}
}

// Allows Player 'O' to make a move
void playerOMove(char array[][3])
{
	while (true) {
		cout << "Pleyer O's turn" << endl;
		int row = getRow();
		int col = getCol();
		// check if space is taken
		if (array[row][col] == '-') { array[row][col] = 'O'; break; }
		cout << "Space already occupied, please select another." << endl;
	}
}

// Function to print boad at whatever state it is in
void printBoard(char array[][3], int rows, int cols)
{
	cout << "Heres the board:" << endl;
	
	for (int i =0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
			cout << array[i][j] << " ";
		cout << endl;
	}
}


Topic archived. No new replies allowed.