call object function for another class

Ok, I first wrote my tictactoe game as one class, and got it to work. I need to split the code up into two classes. Since splitting them up I can't get class tictactoe to read my makeMove or checkWin functions in my board class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #ifndef Board_HPP
#define Board_HPP

class Board
{
private:
	//This stores the board state
	char board[3][3];

public:
	Board();
	// Prints the board
	static void print();
	//Places a marker. If it returns false, it couldnt place!
	bool makeMove(int x, int y, char currentPlayer);
	//Returns true if the current player won!
	bool checkWin(char currentPlayer);
	//Gets input from the user
	int getXCoord();
	int getYCoord();
};
#endif 


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
#include "Board.hpp"
#include <iostream>

using namespace std;

Board::Board()
{
	char board[3][3] = { 0 };
}

void Board::print()
{
	char board[3][3] = { { '.', '.', '.' },
	{ '.', '.', '.' },
	{ '.', '.', '.' } };

	cout << " 123\n";
	for (int i = 0; i < 3; i++) {

		cout << i + 1 << board[i][0] << board[i][1] << board[i][2] << endl;
	}
}

bool Board::makeMove(int x, int y, char currentPlayer)
{
	if (board[y][x] != '.') {
		return false;
	}

	board[y][x] = currentPlayer;
	return true;
}



bool Board::checkWin(char currentPlayer)
{
	//Check the rows
	for (int i = 0; i < 3; i++) {
		if ((board[i][0] == currentPlayer) && (board[i][0] == board[i][1]) && (board[i][1] == board[i][2])) {
			return true; //We won!
		}
	}

	//Check the columns
	for (int i = 0; i < 3; i++) {
		if ((board[0][i] == currentPlayer) && (board[0][i] == board[1][i]) && (board[1][i] == board[2][i])) {
			return true; //We won!
		}
	}

	//Check top left diagonal
	if ((board[0][0] == currentPlayer) && (board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) {
		return true; //We won!
	}

	//Check top right diagonal
	if ((board[2][0] == currentPlayer) && (board[2][0] == board[1][1]) && (board[1][1] == board[0][2])) {
		return true; //We won!
	}

	return false;
}


int Board::getXCoord()
{
	bool isInputBad = true;

	int x;

	while (isInputBad == true) {
		cout << "Enter the X coordinate: ";
		cin >> x;

		if (x < 1 || x > 3) {
			cout << "Invalid Coordinate!\n";
		}
		else {
			isInputBad = false;
		}
	}
	return x - 1;
}

int Board::getYCoord()
{
	bool isInputBad = true;

	int y;

	while (isInputBad == true) {
		cout << "Enter the Y coordinate: ";
		cin >> y;

		if (y < 1 || y > 3) {
			cout << "Invalid Coordinate!\n";
		}
		else {
			isInputBad = false;
		}
	}
	return y - 1;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef TicTacToe_HPP
#define TicTacToe_HPP
#include "Board.hpp"

class TicTacToe
{
public:

	//This plays the game
	void play();
};

#endif


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
#include "TicTacToe.hpp"
#include <iostream>

using namespace std;


void TicTacToe::play()
{

	char player1 = 'X';
	char player2 = 'O';

	char currentPlayer = player1;
	bool isDone = false;

	int x, y;

	int turn = 0;

	//Inner game loop
	while (isDone == false) {

		//Print out the board each time we loop
		Board::print();


		//Get the coordinates from Board getXCoord and           
                //GetYCoord for where the user wants to go
		Board::getXCoord() = x;
		Board::getYCoord() = y;

		//Try to place a marker
		if (Board::makeMove(x, y, currentPlayer) == false) {
			//If we failed to place a marker, tell him he failed!
			cout << "That spot is occupied!\n";
		}
		else {
			//Otherwise, we successfully did this turn!
			turn++;
			//See if the player won!
			if (Board::checkWin(currentPlayer) == true) {
				//He won!
				cout << "The game is over! Player " << currentPlayer << " has won!\n";
				isDone = true;
			}
			else if (turn == 9) {
				//Tie game!
				cout << "Its a tie game!\n";
				isDone = true;
			}


			// Switch players
			if (currentPlayer == player1) {
				currentPlayer = player2;
			}
			else {
				currentPlayer = player1;
			}
		}
	}
}


int main()
{
	char input;
	bool isDone = false;
	//here is our game object
	TicTacToe game;

	//Outer Game Loop
	while (isDone == false) {
		//this plays a game of tic tac toe!
		game.play();
		// We have to see if they want to play again!
		cout << "Would you like to play again? (Y/N): ";
		cin >> input;
		if (input == 'N' || input == 'n') {
			isDone = true;
		}
	}

	return 0;
}
Last edited on
board.h line 3: print should not be static.

board.cpp line 8: This line does nothing. You're declaring a local array that goes out of scope when the constructor exits.

board.cpp line 13: Ditto

Nowhere do I see you instantiate Board.

tictactoe.cpp lines 24,29,30,33,41: All occurrences of Board:: should refer to an instance of Board.

Line 29,30: Your assignments are backwards. x and y should be on the left side.
Thanks for your help!!!!
Topic archived. No new replies allowed.