How do I use a function's return value?

I have been trying to get a return value from my logic function in my Ticktacktoe program. I just can't figure out how to return and check returned values properly. Also, I'm trying to keep my code under 100 lines, so any help on efficient coding would be appreciated.

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
#include <iostream>
using namespace std;
const int ROWS(5);
const int COLS(5);
void draw(char board[ROWS][COLS]) {
	for (int i(0); i < ROWS; i++) {
		for (int j(0); j < COLS; j++)
			cout << board[i][j];
		cout << endl; }
	cout << "Press a corresponding numpad key to place an X or O."; }
void input(char board[ROWS][COLS], char playerTurnX, char playerTurnO, bool playerXTurn) {
	bool error(false);
	do {
		int playerChoice(0);
		cout << endl;
		cin >> playerChoice;
		if (playerChoice > 6 && playerChoice < 10)
			playerChoice -= 6;
		else if (playerChoice < 4 && playerChoice > 0)
			playerChoice += 6;
		int playerROW = ((playerChoice - 1) / 3) * 2;
		int playerCOL = ((playerChoice - 1) % 3) * 2;
		if (board[playerROW][playerCOL] == ' ') {
			if (playerXTurn == true)
				board[playerROW][playerCOL] = playerTurnX;
			else
				board[playerROW][playerCOL] = playerTurnO;
			error = false; }
		else {
			cout << "Error, that spot has already been picked. Try again.";
			error = true; }
	} while (error == true); }
int logic(char board[ROWS][COLS], char playerTurnX, char playerTurnO, bool playerXTurn) {
	int logicWinCheck;
	for (int i(0); i < ROWS; i++) {
		if (board[i][0] == playerTurnX && board[i][2] == playerTurnX && board[i][4] == playerTurnX) {
			logicWinCheck = 2;
			return logicWinCheck; }
		else if (board[i][0] == playerTurnO && board[i][2] == playerTurnO && board[i][4] == playerTurnO) {
			logicWinCheck = 1;
			return logicWinCheck; } }
	for (int j(0); j < COLS; j++) {
		if (board[0][j] == playerTurnX && board[2][j] == playerTurnX && board[4][j] == playerTurnX) {
			logicWinCheck = 2;
			return logicWinCheck; }
		else if (board[0][j] == playerTurnO && board[2][j] == playerTurnO && board[4][j] == playerTurnO) {
			logicWinCheck = 1;
			return 1; } }
	if (board[0][0] == playerTurnX && board[2][2] == playerTurnX && board[4][4] == playerTurnX) {
		logicWinCheck = 2;
		return 2; }
	else if (board[0][0] == playerTurnO && board[2][2] == playerTurnO && board[4][4] == playerTurnO) {
		logicWinCheck = 1;
		return 1; }
	else if (board[4][0] == playerTurnX && board[2][2] == playerTurnX && board[0][4] == playerTurnX) { 
		logicWinCheck = 2;
		return 2; }
	else if (board[4][0] == playerTurnO && board[2][2] == playerTurnO && board[0][4] == playerTurnO) {
		logicWinCheck = 1;
		return 1; }
	else
		return 0; }
int main() {
	char playAgain('N');
	do {
		char board[ROWS][COLS];
		for (int i(0); i < ROWS; i++)
			for (int j(0); j < COLS; j++) {
				if (i == 1 || i == 3 || j == 1 || j == 3)
					board[i][j] = '#';
				else
					board[i][j] = ' '; }
		char playerTurnO('O');
		char playerTurnX('X');
		bool playerXTurn(true);
		bool gameRunning(true);
		int winCheck = 0;
		winCheck = logic(board, playerTurnX, playerTurnO, playerXTurn);
		while (gameRunning) {
			draw(board);
			input(board, playerTurnX, playerTurnO, playerXTurn);
			logic(board, playerTurnX, playerTurnO, playerXTurn);
			if (playerXTurn == true)
				playerXTurn = false;
			else
				playerXTurn = true;
			system("CLS");
			if (winCheck == 2) {
				draw(board);
				cout << "X wins! Play again? [Y/N] ";
				cin >> playAgain; }
			else if (winCheck == 1) {
				draw(board);
				cout << "O wins! Play again? [Y/N] ";
				cin >> playAgain; } }
	} while (playAgain == 'Y' || playAgain == 'y');
	return(0); } 
IMO, your functions are doing too much. Each function should do a little as possible but do it well.

For example your
int logic(char board[ROWS][COLS], char playerTurnX, char playerTurnO, bool playerXTurn)
Seems to be a bit confusing.

Wouldn't it be better just to pass in the board, and the current player? After all only the current player can "win". Also since this function returns a value wouldn't it be helpful to actually use the value this function returned?


Next why all the magic numbers scattered throughout the program.

Edit: Also why are you using such a large board. A "normal" tick-tack-toe board is 3 by 3.
Last edited on
Topic archived. No new replies allowed.