the problem lies when the move checks to make sure it is valid, every move will display valid or not valid, and the code always displays not valid everytime
#include <iostream>
#include <fstream>
using namespace std;
int initialmenu();
void initialboard(char board[][8]);
int moveoptions();//function prototypes
void appropriatemove(char board[][8], int move, char piece, int& w, int& row, int& column, int& newrow, int& newcolumn);
void movingapiece(char board[][8], int row, int column, int newrow, int newcolumn, char piece);
int anymovesleft(char board[][8]);
void assigningpoints(char piece, char board[][8], int newrow, int newcolumn, int player, int& points1, int& points2);
void savingagame(char board[][8], int players);
int movesleft(int row, int column, char board[][8]);
void resumingagame(char board[][8], int& players);
int main()
{
int choice = 0, w = 0, move, winner = 0, row=0, column=0, newrow, newcolumn, player, points1 = 0, points2 = 0, p;
int players, choice2, i;//declare variables
char board[8][8], piece;
while (choice != 4)
{
choice = initialmenu();//call function to diplay a menu
switch (choice)
{
case 1: initialboard(board);
players = 1;//move for player 1
while (winner != 1)
{
cout << "Choose one" << endl;//display menu
cout << "1. Make a move" << endl;
cout << "2. Save the game" << endl;
cout << "3. Quit" << endl;
cin >> choice2;//take in choice
while (choice2 != 3)
{
switch (choice2)
{
case 1:
while (w != 1)
{
player = 1;//assign player to 1
cout << "Player 1 Would you like to move piece A, B, C, or D?" << endl;
cin >> piece;//ask user which piece and take it in
move = moveoptions();//call functions
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
}
assigningpoints(piece, board, newrow, newcolumn, player, points1, points2);
movingapiece(board, row, column, newrow, newcolumn, piece);
p = anymovesleft(board);//assign p to return value of function
if (p > 0)//if p > 0 end of game
{
cout << "game over" << endl;
winner = 1;//assign winner to 1 to end while loop
if (points1 > points2)//decide who the winner is
cout << "Player one wins, " << points1 << " to " << points2 << endl;
else {
if (points2 > points1)//display winner
cout << "Player two wins, " << points2 << " to " << points1 << endl;
else cout << "tie game, " << points1 << " & " << points2 << endl;
}
}
while (w != 1)//computer gets to move
{
for (i = 0; i < 8; i++)
{
piece = 'A';
move = i;//choose a move from all possible moves
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
if (w == 1)
i = 9;
}
if(w != 1)//continue only if previous moves are unavailable
{
for (i = 0; i < 8; i++)
{
piece = 'B';//use for loop to run through all possible moves
move = i;
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
if (w == 1)
i = 9;
}
}
if (w != 1)
{
for (i = 0; i < 8; i++)
{
piece = 'C';
move = i;
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
if (w == 1)
i = 9;
}
}
if (w != 1)//repeat as above
{
for (i = 0; i < 8; i++)
{
piece = 'D';
move = i;
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
if (w == 1)
i = 9;
}
}
}//call functions
assigningpoints(piece, board, newrow, newcolumn, player, points1, points2);
movingapiece(board, row, column, newrow, newcolumn, piece);
p = anymovesleft(board);//assign p to return value
if (p > 0)//determine if there is a winner
{
cout << "game over" << endl;
winner = 1;//assign winner to 1 to end while loop
if (points1 > points2)//display winner and points
cout << "Player one wins, " << points1 << " to " << points2 << endl;
else {
if (points2 > points1)
cout << "Player two wins, " << points2 << " to " << points1 << endl;
else cout << "tie game, " << points1 << " & " << points2 << endl;
}//end else statement
}//end if statement
break;
case 2: savingagame(board, players);//go to function
break;
case 3: cout << "Good-bye" << endl;//end switch statment
break;
default: cout << "Invalid" << endl;//display error message
}//end switch(choice2)
}//end while(choice2 != 3)
}//end while(winner!=1)
break;
case 2: initialboard(board);//call function
players = 2;//assign number of players
while (winner != 1)
{
cout << "Choose one" << endl;//display menu
cout << "1. Make a move" << endl;
cout << "2. Save the game" << endl;
cout << "3. Quit" << endl;
cin >> choice2;//take in choice
while (choice2 != 3)
{
switch (choice2)
{
case 1:while (w != 1)
{
player = 1;//move for player 1
cout << "Player 1 Would you like to move piece A, B, C, or D?" << endl;
cin >> piece;//ask user what piece to move
move = moveoptions();//call functions
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
}
assigningpoints(piece, board, newrow, newcolumn, player, points1, points2);
movingapiece(board, row, column, newrow, newcolumn, piece);
p = anymovesleft(board);//assign p to return value
if (p > 0)//determine if game is over
{
cout << "game over" << endl;
winner = 1;//to get out of while loop
if (points1 > points2)
cout << "Player one wins, " << points1 << " to " << points2 << endl;
else{
if (points2 > points1)//diplay winner and points
cout << "Player two wins, " << points2 << " to " << points1 << endl;
else cout << "tie game, " << points1 << " & " << points2 << endl;
}//end else statment
}//end if statment
while (w != 1)
{
player = 2;//move for player 2
cout << "Player 2 Would you like to move piece A, B, C, or D?" << endl;
cin >> piece;//what piece to move
move = moveoptions();//call functions
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
}
assigningpoints(piece, board, newrow, newcolumn, player, points1, points2);
movingapiece(board, row, column, newrow, newcolumn, piece);
p = anymovesleft(board);//assign p to return value
if (p > 0)
{
cout << "game over" << endl;
winner = 1;//get out of while loop
if (points1 > points2)
cout << "Player one wins, " << points1 << " to " << points2 << endl;
else{
if (points2 > points1)//display winner and points
cout << "Player two wins, " << points2 << " to " << points1 << endl;
else cout << "tie game, " << points1 << " & " << points2 << endl;
}//end else statment
}//end if statment
break;
case 2: savingagame(board, players);//call function
break;
case 3: cout << "Good-bye" << endl;//end switch statment
break;
default: cout << "Invalid" << endl;//display error message
}//end switch(choice2)
}// end while(choice2!= 3)
}//end while(winner != 1)
break;
case 3:resumingagame(board, players);//call function
while (winner != 1)
{
cout << "Choose one" << endl;//display menu
cout << "1. Make a move" << endl;
cout << "2. Save the game" << endl;
cout << "3. Quit" << endl;
cin >> choice2;//take in choice
while (choice2 != 3)
{
switch (choice2)
{
case 1:while (w != 1)
{
player = 1;//move for player 1
cout << "Would you like to move piece A, B, C, or D?" << endl;
cin >> piece;//what piece to move
move = moveoptions();//call functions
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
}
assigningpoints(piece, board, newrow, newcolumn, player, points1, points2);
movingapiece(board, row, column, newrow, newcolumn, piece);
p = anymovesleft(board);//assign p to return value
if (p > 0)//determine if game over
{
cout << "game over" << endl;
winner = 1;//get out of while loop
if (points1 > points2)
cout << "Player one wins, " << points1 << " to " << points2 << endl;
else{
if (points2 > points1)//display winner and points
cout << "Player two wins, " << points2 << " to " << points1 << endl;
else cout << "tie game, " << points1 << " & " << points2 << endl;
}//end else statment
}//end if statment
if(players = 1)//determine players if one player...
{
while (w != 1)
{
for (i = 0; i < 8; i++)
{
piece = 'A';
move = i;//find move for computer
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
if (w == 1)
i = 9;//get out of while loop when a move is found
}
if(w != 1)
{
for (i = 0; i < 8; i++)
{
piece = 'B';
move = i;//look at all possible moves
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
if (w == 1)
i = 9;
}
}
if (w != 1)
{
for (i = 0; i < 8; i++)
{
piece = 'C';
move = i;
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
if (w == 1)
i = 9;
}
}
if (w != 1)
{
for (i = 0; i < 8; i++)
{
piece = 'D';
move = i;
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
if (w == 1)
i = 9;
}
}
}//call functions
assigningpoints(piece, board, newrow, newcolumn, player, points1, points2);
movingapiece(board, row, column, newrow, newcolumn, piece);
p = anymovesleft(board);
if (p > 0)
{
cout << "game over" << endl;
winner = 1;//get out of while loop
if (points1 > points2)
cout << "Player one wins, " << points1 << " to " << points2 << endl;
else {
if (points2 > points1)//display winner and points
cout << "Player two wins, " << points2 << " to " << points1 << endl;
else cout << "tie game, " << points1 << " & " << points2 << endl;
}//end else statement
}//end if statement
}//end if(players = 1)
else { //if 2 players...
while (w != 1)
{
player = 2;//go for player 2
cout << "Player 2 Would you like to move piece A, B, C, or D?" << endl;
cin >> piece;//what piece to move
move = moveoptions();//call functions
appropriatemove(board, move, piece, w, row, column, newrow, newcolumn);
}
assigningpoints(piece, board, newrow, newcolumn, player, points1, points2);
movingapiece(board, row, column, newrow, newcolumn, piece);
p = anymovesleft(board);//assign p to return value
if (p > 0)//determine if game over
{
cout << "game over" << endl;
winner = 1;//get out of while loop
if (points1 > points2)
cout << "Player one wins, " << points1 << " to " << points2 << endl;
else{
if (points2 > points1)//display winner and points
cout << "Player two wins, " << points2 << " to " << points1 << endl;
else cout << "tie game, " << points1 << " & " << points2 << endl;
}//end else statment
}//end if statment
}//end else statment
break;
case 2: savingagame(board, players);//call function
break;
case 3: cout << "Good-bye" << endl;
break;
default: cout << "Invalid" << endl;
}//end switch(choice2)
}//end while(choice2 != 3)
}// end while(winner != 1)
break;
case 4:cout << "Good-bye" << endl;//get out of switch statment
break;
default: cout << "Invalid" << endl;//display error message
}
}
return 0;//end main
}
int initialmenu()
{
int choice;//declare variables
cout << "Please choose one" << endl;//display menu
cout << "1. Start a New One Player Game" << endl;
cout << "2. Start a New Two Player Game" << endl;
cout << "3. Resume an Old Game" << endl;
cout << "4. Quit" << endl;
cin >> choice;//take in choice and return it
return choice;
}
void initialboard(char board[][8])
{
ifstream input;
char initial[8][8];
int i, k;//declare variables
input.open("initialboard.txt");//open input file
for (i = 0; i < 8; i++)//read in initial board
{
for (k = 0; k < 8; k++)
{
input >> initial[i][k];
}
}
for (i = 0; i < 8; i++)//display initial board
{
for (k = 0; k < 8; k++)
{
cout << initial[i][k];
}
cout << endl;
}
return;
}
int moveoptions()
{
int choice;//declare variable
cout << "Please choose a move" << endl;
cout << " 1 2 " << endl;//display move options
cout << "3 4" << endl;
cout << " X " << endl;
cout << "5 6" << endl;
cout << " 7 8 " << endl;
cin >> choice;//take in choice and return it
return choice;
}
void appropriatemove(char board[][8], int move, char piece, int& w, int& row, int& column, int& newrow, int& newcolumn)
{
int i, k;//declare variables
for (i = 0; i < 8; i++)
{
for (k = 0; k < 8; k++)
{
if (board[i][k] == piece)
{
row = i;//find piece on board
column = k;
}
}
}
switch (move)
{//determine if it is an acceptable move
case 1: if (row - 2 >= 0 && row - 2 <= 9 && column - 1 >= 0 && column - 1 <= 8)
{ if(board[row-2][column-1] == '-' || board[row-2][column-1] == '|')
{
cout << "That is a valid move" << endl;
newrow = row -2;//if good move display it, hold that spot, add one to w
newcolumn = column -1;
w++;
}
}
else cout << "Invalid move" << endl;//display error message
break;
case 2: if (row - 2 >= 0 && row - 2 <= 9 && column + 1 >= 0 && column + 1 <= 8)
{
if(board[row-2][column+1] == '-' || board[row-2][column+1] == '|')
{
cout << "That is a valid move" << endl;
newrow = row - 2;
newcolumn = column + 1;
w++;
}
}
else cout << "Invalid move" << endl;
break;
case 3: if (row - 1 >= 0 && row - 1 <= 9 && column - 2 >= 0 && column - 2 <= 8)
{
if(board[row-1][column-2] == '-' || board[row-1][column-2] == '|')
{
cout << "That is a valid move" << endl;
newrow = row - 1;
newcolumn = column - 2;
w++;
}
}
else cout << "Invalid move" << endl;//if not valid move display error message
break;
case 4: if (row - 1 >= 0 && row - 1 <= 9 && column + 2 >= 0 && column + 2 <= 8)
{
if(board[row-1][column+2] == '-' || board[row-1][column+2] == '|')
{
cout << "That is a valid move" << endl;
newrow = row - 1;
newcolumn = column + 2;
w++;
}
}
else cout << "Invalid move" << endl;
break;
case 5: if (row + 1 >= 0 && row + 1 <= 9 && column - 2 >= 0 && column - 2 <= 8)
{
if(board[row+1][column-2] == '-' || board[row+1][column-2] == '|')
{
cout << "That is a valid move" << endl;
newrow = row + 1;
newcolumn = column - 2;
w++;
}
}
else cout << "Invalid move" << endl;
break;
case 6: if (row + 1 >= 0 && row + 1 <= 9 && column + 2 >= 0 && column + 2 <= 8)
{
if(board[row+1][column+2] == '-' || board[row+1][column+2] == '|')
{
cout << "That is a valid move" << endl;
newrow = row + 1;
newcolumn = column + 2;
w++;
}
}
else cout << "Invalid move" << endl;
break;
case 7: if (row + 2 >= 0 && row + 2 <= 9 && column - 1 >= 0 && column - 1 <= 8)
{
if(board[row+2][column-1] == '-' || board[row+2][column-1] == '|')
{
cout << "That is a valid move" << endl;
newrow = row + 2;
newcolumn = column - 1;
w++;
}
}
else cout << "Invalid move" << endl;
break;
case 8: if (row + 2 >= 0 && row + 2 <= 9 && column + 1 >= 0 && column + 1 <= 8)
{
if(board[row+2][column+1] == '-' || board[row+2][column+1] == '|')
{
cout << "That is a valid move" << endl;
newrow = row + 2;
newcolumn = column + 1;
w++;
}
}
else cout << "Invalid move" << endl;
break;
}
return;
}
void movingapiece(char board[][8], int row, int column, int newrow, int newcolumn, char piece)
{
int i, k;
board[row][column] = 'X';//move piece and mark old piece
board [newrow][newcolumn] = piece;
for(i = 0; i < 8; i++)
{//display new board
for(k = 0; k < 8; k++)
{
cout << board[i][k];
}
cout << endl;
}
return;
}
void assigningpoints(char piece, char board[][8], int newrow, int newcolumn, int player, int& points1, int& points2)
{
if(piece == 'A' || piece == 'D')//if the piece moved is a or d
{
if(board[newrow][newcolumn] == '-')//if the piece coresponds with the space
{
if (player == 1)//if it is player 1
points1 = points1 + 3;//if all apply then you get _ points
else points2 = points2 + 2;
}
else
{
if(player == 1)//or if the space doesn't correspond to the piece
points1 = points1 + 2;
else points2 = points2 + 2;
}
}
else //or if the piece is c or b
{
if(board[newrow][newcolumn] == '-')//if piece doesn't correspond to space
{
if (player == 1)//if player 1
points1 = points1 + 2;
else points2 = points2 + 2;
}
else //or if piece does correspond to space
{
if (player == 1)//if player 1
points1 = points1 + 1;
else points2 = points2 + 3;
}
}
return;
}
int anymovesleft(char board[][8])
{
int i, k, onerow = 0, onecolumn = 0, tworow = 0, twocolumn = 0, threerow = 0;
int threecolumn = 0, fourrow = 0, fourcolumn = 0, p = 0;//declare variables
for (i = 0; i < 8; i++)
{
for(k = 0; k < 8; k++)
{//find all pieces on the board
if(board[i][k] = 'A')
{
onerow = i;
onecolumn = k;
}
else
{
if(board[i][k] = 'B')
{
tworow = i;
twocolumn = k;
}
else
{
if (board[i][k] = 'C')
{
threerow = i;
threecolumn = k;
}
else
{
if(board[i][k] = 'D')
{
fourrow = i;
fourcolumn = k;
}
else;
}
}
}
}
}
while (p != 1)
{//call function until move is found
p = movesleft(onerow, onecolumn, board);
p = movesleft(tworow, twocolumn, board);
p = movesleft(threerow, threecolumn, board);
p = movesleft(fourrow, fourcolumn, board);
}//return p as refernece to moves avaiable or not
return p;
}
int movesleft(int row, int column, char board[][8])
{
int p = 0;//declare vaiable
while (p != 0)
{//look through all possibilities to see if any moves are left
if (row - 2 >= 0 && row - 2 <= 9 && column - 1 >= 0 && column - 1 <= 8 && (board[row-2][column-1] == '-' || board[row-2][column-1] == '|'))
p = p + 1;
if (row - 2 >= 0 && row - 2 <= 9 && column + 1 >= 0 && column + 1 <= 8 && (board[row-2][column+1] == '-' || board[row-2][column+1] == '|'))
p = p + 1;
if (row - 1 >= 0 && row - 1 <= 9 && column - 2 >= 0 && column - 2 <= 8 && (board[row-1][column-2] == '-' || board[row-1][column-2] == '|'))
p = p + 1;
if (row - 1 >= 0 && row - 1 <= 9 && column + 2 >= 0 && column + 2 <= 8 && (board[row-1][column+2] == '-' || board[row-1][column+2] == '|'))
p = p + 1;
if (row + 1 >= 0 && row + 1 <= 9 && column - 2 >= 0 && column - 2 <= 8 && (board[row+1][column-2] == '-' || board[row+1][column-2] == '|'))
p = p + 1;
if (row + 1 >= 0 && row + 1 <= 9 && column + 2 >= 0 && column + 2 <= 8 && (board[row+1][column+2] == '-' || board[row+1][column+2] == '|'))
p = p + 1;
if (row + 2 >= 0 && row + 2 <= 9 && column - 1 >= 0 && column - 1 <= 8 && (board[row+2][column-1] == '-' || board[row+2][column-1] == '|'))
p = p + 1;
if (row + 2 >= 0 && row + 2 <= 9 && column + 1 >= 0 && column + 1 <= 8 && (board[row+2][column+1] == '-' || board[row+2][column+1] == '|'))
p = p + 1;
}//return p to determine game over
return p;
}
void savingagame(char board[][8], int players)
{
ofstream output;
int i, k;//declare variables
char filename[10];
cout << "Please enter a file name" << endl;
cin >> filename;//ask for and get filename
output.open(filename);//open output file
output << players;//put in number of players
for (i = 0; i < 8; i++)//walk through board to put into file
{
for (k = 0; k < 8; k++)
{
output << board[i][k];
}
output << endl;//hit return to make it look pretty
}
return;
}
void resumingagame(char board[][8], int& players)
{
ifstream input;
char filename[10];
int i, k;//declare variables
cout << "Please enter the file name of the game you wish to continue" << endl;
cin >> filename;//ask for and get filename
input.open(filename);//open file
input >> players;//take in number of players
for(i = 0; i < 8; i++)
{//walk through board to play on
for(k = 0; k < 8; k++)
{
input >> board[i][k];
}
}
for(i = 0; i < 8; i++)
{//display board
for (k = 0; k < 8; k++)
{
cout << board[i][k];
}
cout << endl;
}
return;
}