Hi, I'm a beginner in c++. We were asked to make a reversi game program for two players using loops, char and such. No AI or animation.
I'm having problems with the board. It's not declared and the row, type can't be identified.
#include <cstdlib>
#include <iostream>
using namespace std;
int inputrow;
int inputcol;
char O;
char X;
char col;
char row;
char board [][];
void playerone ()
{
cout << "Enter row(1-8):";
cin >> inputrow;
cout << "Enter column(1-8):";
cin >> inputcol;
if (board[inputrow][inputcol] ==' ')
{
if (board[inputrow-1][inputcol]!='X'||board[inputrow+1][inputcol]!='X'||board[inputrow][inputcol-1]!='X'||board[inputrow][inputcol+1]!='X'||board[inputrow-1][inputcol-1]!='X'||board[inputrow-1][inputcol+1]!='X'||board[inputrow+1][inputcol-1]!='X'||board[inputrow+1][inputcol+1]!='X')
{
do
{
board [inputrow][inputcol]='X';
if(board[inputrow-1][inputcol]='O')
{
board[inputrow][inputcol='X'];
inputrow--;
}
else if (board[inputrow+1][inputcol]='O')
{
board[inputrow+1][inputcol]='X';
inputrow++;
}
else if (board[inputrow][inputcol-1]='O')
{
board[inputrow][inputcol-1]='X';
inputcol--;
}
else if (board[inputrow][inputcol+1]='O')
{
board[inputrow][inputcol+1]='X';
inputcol++;
}
else if (board[inputrow-1][inputcol-1]='O')
{
board[inputrow-1][inputcol-1]='X';
inputrow--;
inputcol--;
}
else if (board[inputrow-1][inputcol+1]='O')
{
board[inputrow-1][inputcol+1]='X';
inputrow--;
inputcol++;
}
else if (board[inputrow+1][inputcol-1]='O')
{
board[inputrow+1][inputcol-1]='X';
inputrow++;
inputcol--;
}
else if (board[inputrow+1][inputcol+1]='O')
{
board[inputrow+1][inputcol+1]='X';
inputrow++;
inputcol++;
}
else
{
break;
}
}
while (board[inputrow-1][inputcol]='O'||board[inputrow+1][inputcol]!='X'||board[inputrow][inputcol-1]!='X'||board[inputrow][inputcol+1]!='X'||board[inputrow-1][inputcol-1]!='X'||board[inputrow-1][inputcol+1]!='X'||board[inputrow+1][inputcol-1]!='X'||board[inputrow+1][inputcol+1]!='X');
}
}
}
int player1counter=0;
for (row=1;row<9;row++)
{
for (col=1;col<9;col++)
if (board [row][col]=='X')
player1counter++;
}
else
cout <<"Invalid move. Choose another set of coordinates."<<endl;
}
}
void playertwo ()
{
cout << "Enter row(1-8):";
cin >> inputrow;
cout << "Enter column(1-8):";
cin >> inputcol;
if (board[inputrow][inputcol]==' ')
{
if (board[inputrow-1][inputcol]!='O'||board[inputrow+1][inputcol]!='O'||board[inputrow][inputcol-1]!='O'||board[inputrow][inputcol+1]!='O'||board[inputrow-1][inputcol-1]!='O'||board[inputrow-1][inputcol+1]!='O'||board[inputrow+1][inputcol-1]!='O'||board[inputrow+1][inputcol+1]!='O')
{
do
{
board [inputrow][inputcol]='O';
if(board[inputrow-1][inputcol]='X')
{
board[inputrow][inputcol='O'];
inputrow--;
}
else if (board[inputrow+1][inputcol]='X')
{
board[inputrow+1][inputcol]='O';
inputrow++;
}
else if (board[inputrow][inputcol-1]='X')
{
board[inputrow][inputcol-1]='O';
inputcol--;
}
else if (board[inputrow][inputcol+1]='X')
{
board[inputrow][inputcol+1]='O';
inputcol++;
}
else if (board[inputrow-1][inputcol-1]='X')
{
board[inputrow-1][inputcol-1]='O';
inputrow--;
inputcol--;
}
else if (board[inputrow-1][inputcol+1]='X')
{
board[inputrow-1][inputcol+1]='O';
inputrow--;
inputcol++;
}
else if (board[inputrow+1][inputcol-1]='X')
{
board[inputrow+1][inputcol-1]='O';
inputrow++;
inputcol--;
}
else if (board[inputrow+1][inputcol+1]='X')
{
board[inputrow+1][inputcol+1]='O';
inputrow++;
inputcol++;
}
else
{
break;
}
}
while (board[inputrow-1][inputcol]='O'||board[inputrow+1][inputcol]!='X'||board[inputrow][inputcol-1]!='X'||board[inputrow][inputcol+1]!='X'||board[inputrow-1][inputcol-1]!='X'||board[inputrow-1][inputcol+1]!='X'||board[inputrow+1][inputcol-1]!='X'||board[inputrow+1][inputcol+1]!='X');
}
}
}
int playertwocounter=0;
for (row=0;row<9;row++)
{
for (col=0;col<9;col++)
if (board [row][col]=='O')
playertwocounter++;
}
else
cout <<"Invalid move. Choose another set of coordinates."<<endl;
}
}
int main ()
{
bool newgame;
do
{
cout<< "Hello, this is a Reversi Game for Two players." << endl << "Player 1 is X. Player 2 is O." << endl;
for (row=0; row<9; row++) //rows
{
for (col=0; col<9; col++) //columns
board [row][col]= ' ';
}
board [4][4]= 'O';
board [5][5]= 'O';
board [5][4]= 'X';
board [4][5]= 'X';
board [0][0]= '0';
board [0][1]= '1';
board [0][2]= '2';
board [0][3]= '3';
board [0][4]= '4';
board [0][5]= '5';
board [0][6]= '6';
board [0][7]= '7';
board [0][8]= '8';
board [1][0]= '1';
board [2][0]= '2';
board [3][0]= '3';
board [4][0]= '4';
board [5][0]= '5';
board [6][0]= '6';
board [7][0]= '7';
board [8][0]= '8';
}
for (row=0; row<9; row++)
{
cout << "------------------" << endl;
for (col=0; col<9; col++)
{
cout << "|" << board [row][col];
}
{
cout << "|";
}
cout << endl;
}
cout << "------------------" << endl;
int playermovecounter=0;
for(;;)
{
if (player+=2)
playerone ();
else
playertwo ();
}
cout<< "Score:" <<endl<< "Player 1: "<< player1counter << endl<< "Player 2: " << player2counter<<endl;
if (player1counter>player2counter)
cout<< "Player 1 wins!" <<endl;
else if (player2counter>player1counter)
cout<< "Player 2 wins!" <<endl;
else if (player2counter==player1counter)
cout<< "It's a draw." << endl;
cout<< "Do you want to play again? For new game, press 1. For exit, press E" <<endl;
cin>> newgame;
if (newgame==1)
newgame=true;
system ("cls");
} while (newgame==1);
if (newgame=false)
system ("exit");
return EXIT_SUCCESS;
}
You have to specify the size of array when you declare a variable, i.e.
|
char board[9][9]; // I assume the size of your board is 9x9
|
I didn't look through the rest of your code - it's very messy, but perhaps it works :)
PS: please use code tags when posting source code.
Last edited on