alright, I have most of this hammered out, but still a couple problems I cant figure out. in the player class, the Nextmove function is not working for me. It uses the setvalue function in TicTacToe class(which works fine by itsself)
also there are problems assigning player names that I cant figure out either, one name will turn out fine, and the other will end up being a concatinization of its name and the other name minus a char or 2. I have it setup so they can input row 1-3 instead of 0-2(array) and it subtracts 1 from what they input to put inthe the char array. Heres the code:
2 drivers(test.c and game.cpp) just been using test.c for small tests and game will be the main driver.
test.c
#include <iostream>
#include "Player.h"
//#include "TicTacToe.h"
using namespace std;
int main()
{
Player p1("Ralph", 1);
Player p2("Joe", 2);
TicTacToe tic;
p1.NextMove(tic);
tic.setValue(1, 1, 1);
tic.setValue(3, 3, 2);
tic.print();
//cout << "\n" << p1.GetName() << "\n";
return 0;
}
game.cpp
#include <iostream>
#include "Player.h"
#include <time.h>
using namespace std;
char * setname(int, char *);
int doindex(int);
int main()
{
int myindex = 1, row, col;
TicTacToe tic;
char name[20];
Player * turnplayer;
cout << "Welcome to TicTacToe(Edge Style!) version 1.00\n";
Player p1(setname(1, name), 1);
Player p2(setname(2, name), 2);
cout << "\nPlayer 1 is x, Player 2 is o.\n";
if(((int)clock()) % 2 == 0) myindex = -1;
else myindex = 1;
cout << "\nPlayer " << doindex(myindex) << " win the toss, you go goes first.\n\n";
while(tic.getStatus() == 3)
{
tic.print();
cout << "\n\n" << myindex << "\n";
if (myindex == -1) p1.NextMove(tic);
else p2.NextMove(tic);
myindex = myindex * -1;
}
switch(tic.getStatus())
{
case 0:
cout << "The Game is a Tie";
break;
case 1:
cout << "Player 1 wins!";
break;
case 2:
cout << "Player 2 wins!";
break;
}
return 0;
}
char * setname(int index, char * myname)
{
cout << "\nPlayer " << index << ", Please enter your name: ";
cin >> myname;
//cout << "--" << myname;
return myname;
}
inline int doindex(int a)
{
if(a = -1) return 2;
else return 1;
}
TicTacToe.h
#include <iostream>
using namespace std;
class TicTacToe
{
public:
void print(); //prints out the gameboard at any time
int getStatus(); //returns tie, win for either player, or neither
bool setValue(int, int, int); // (row, column, player index) makes a move for the player, true if the move $
TicTacToe(); //constructor
char board[3][3]; //2d array for game board
private:
bool iswin(int); //input player index to see if they have won the game
bool isEmpty(int, int); //true if the space is empty
char charspace(char); //returns a space for \0 char, else returns the char inputted
};
TicTacToe.cpp
#include <iostream>
#include "TicTacToe.h"
using namespace std;
//char board[3][3];
bool TicTacToe::iswin(int index)
{
int x;
char psymbol;
if(index == 1) psymbol = 'x';
else psymbol = 'o';
for(x = 0; x < 3; x++) if (board[x][0] == psymbol && board[x][1] == psymbol && board[x][2] == psymbol) return true; $
for(x = 0; x < 3; x++) if (board[0][x] == psymbol && board[1][x] == psymbol && board[2][x] == psymbol) return true; $
if(board[0][0] == psymbol && board[1][1] == psymbol && board[2][2] == psymbol) return true; //check 1st diagonal
if(board[0][2] == psymbol && board[1][1] == psymbol && board[2][0] == psymbol) return true; //check other diagon$
return false;
}
int TicTacToe::getStatus()
{
if(iswin(1) == true) return 1; //player 1 wins
else if(iswin(2) == true) return 2; //player 2 wins
else
{
int x, y;
for(x = 0; x < 3; x++) //search for \0 chars to see if board is full
{
for(y = 0; y < 3; y++)
{
if (board[x][y] == '\0') return 3; //board is not full, neither player has won, keep playing
}
}
return 1; //if it makes it to this line, the game is a tie
}
}
char TicTacToe::charspace(char a)
{
if (a == '\0') return ' ';
else return a;
}
void TicTacToe::print()
{
int x;
cout <<"\t-------------\n";
for(x = 0; x < 3; x++)
{
cout << "\t" << "| " << charspace(board[x][0]) << " | " << charspace(board[x][1]) << " | " << charspace(b$
cout <<"\t-------------\n";
}
}
bool TicTacToe::isEmpty(int row, int col)
{
if (board[row-1][col-1] == '\0') return true;
else return false;
}
bool TicTacToe::setValue(int row, int col, int player)
{
if(row < 4 && col < 4 && row > 0 && col > 0)
{
if(isEmpty(row, col) == true)
{
char a;
if(player == 1) a = 'x';
else a = 'o';
board[row-1][col-1] = a;
cout << "\n\n" << a << "\n";
return true;
}
else
{
cout << "\nSpace already occupied. Try again.\n";
return false;
}
}
else
{
cout << "\nInvalid position. Try again.\n";
return false;
}
}
TicTacToe::TicTacToe()
{
int x, y;
for(x = 0; x < 3; x++)
{
for(y = 0; y < 3; y++)
{
board[x][y] = '\0';
}
}
}
Player.h
#include <iostream>
#include "TicTacToe.h"
using namespace std;
class Player
{
public:
Player(char *, int);
int GetIndex(); //returns player index
char * GetName(); //returns player name
void NextMove(TicTacToe x); //for a player to make a move
int pindex; //player index
char name[]; //player name
private:
};
Player.cpp
#include <iostream>
#include "Player.h"
#define MAX 20
using namespace std;
Player::Player(char *pname, int pnumber)
{
int *ptr;
/*int x;
for(x = 0; x < 20; x++)
{
name[x] = *(pname + x);
}*/
pindex = pnumber;
strcpy(name, pname);
cout << "INDEX!!!! " << pindex << endl;
}
int Player::GetIndex()
{
return pindex;
}
char * Player::GetName() { return name; }
void Player::NextMove(TicTacToe toe)
{
int row, col;
do
{
cout << "\n" << name << ": Enter the Row and Column for your next move: ";
cin >> row >> col;
cout << "\n" << pindex;
}
while (toe.setValue(row, col, pindex) == false);
}
Thanks for any help