我是C ++的初学者,通过“跳入C ++”工作。我正在学习数组,并且正在尝试将基本的跳棋游戏放在一起,而且我有一段时间让我的初始数组代表棋盘正常工作。特别是,第10行显示了我不希望的值。我希望这些值是:
[10] [2] [10] [2] [10] [2] [10] [2] [10] [2]
相反,它是:
[10] [2] [10] [2] [10] [ 10 ] [10] [2] [10] [ 1 ](突出显示两个未按预期出现的值)。
我通过显示几个元素的值来确认第265和288行(标记为“// WTF?”)的值,并得到: 空间6,10 = 0 空间10,10 = 1
基于我用来设置初始值的“for”语句,我不明白为什么这两个值设置不正确。
感谢任何帮助!
谢谢!
#include <iostream>
using namespace std;
//intializing functions
string displaySpace( int array[10][10], int x, int y );
bool validPlayerOneMoveCheck( int array[][10], int player, int piece_column, int piece_row, int move_column, int move_row );
bool validPlayerTwoMoveCheck( int array[][10], int player, int piece_column, int piece_row, int move_column, int move_row );
void movePiece( int array[][10], int player, int otherPlayer, int piece_column, int piece_row, int move_column, int move_row );
void jumpPiece( int array[][10], int player, int otherPlayer, int piece_column, int piece_row, int move_column, int move_row );
//void checkKing ( int array[][10], int player, int move_column, int move_row );
int otherPlayerDefine( int player )
{
int otherPlayer = 0;
if ( player == 1 )
{
otherPlayer = 2;
}
else
{
otherPlayer = 1;
}
return otherPlayer;
}
bool validPieceCheck( int array[10][10], int player, int piece_column, int piece_row )
{
bool validPiece = 0;
if ( piece_column < 1 || piece_column > 10 || piece_row < 1 || piece_row > 10 )
{
cout << "You have entered an invalid space number.\n";
}
else if ( array[piece_row][piece_column] == player || array[piece_row][piece_column] == player + 2 )
{
validPiece = 1; // this is a valid space and a valid piece
}
else
{
cout << "You do not have a piece at position " << piece_column << ", " << piece_row << "\n";
}
return validPiece;
}
bool validMoveCheck( int array[10][10], int player, int piece_column, int piece_row, int move_column, int move_row )
{
bool validMove = 0;
if ( move_column < 1 || move_column > 10 || move_row < 1 || move_row > 10 || array[move_row][move_column] == 10 )
{
cout << "You have entered an invalid space number.\n";
}
else if ( array[move_row][move_column] != 0 )
{
cout << "That space is already occupied.\n";
}
else if ( array[piece_row][piece_column] == 1 ) //player one normal piece
{
validMove = validPlayerOneMoveCheck( array, player, piece_column, piece_row, move_column, move_row );
cout << "Player One Move Check, normal piece\n";
}
else if ( array[piece_row][piece_column] == 2 ) //player two normal piece
{
validMove = validPlayerTwoMoveCheck( array, player, piece_column, piece_row, move_column, move_row );
cout << "Player Two Move Check, normal piece\n";
}
else if ( array[piece_row][piece_column] == 3 || array[piece_row][piece_column] == 4 ) //either player king
{
bool validMove01 = validPlayerOneMoveCheck( array, player, piece_column, piece_row, move_column, move_row );
bool validMove02 = validPlayerTwoMoveCheck( array, player, piece_column, piece_row, move_column, move_row );
cout << "King Piece Move Check\n";
if ( validMove01 == 1 || validMove02 == 1 )
{
validMove = 1;
}
}
else
{
cout << "That is not a valid move\n";
}
cout << "validMove = " << validMove << "\n";
//check if kinged
//checkKing ( array, player, move_column, move_row );
return validMove;
}
//player 1 valid move check from top of board (low numbers) to bottom (higher numbers)
bool validPlayerOneMoveCheck( int array[][10], int player, int piece_column, int piece_row, int move_column, int move_row )
{
bool validMove;
int otherPlayer = otherPlayerDefine( player );
//moving one space to empty space; empty space verified in ValidMoveCheck already
if (( move_column == piece_column + 1 || move_column == piece_column - 1) && ( move_row == piece_row + 1 ))
{
validMove = 1;
movePiece( array, player, otherPlayer, piece_column, piece_row, move_column, move_row );
}
//jumping another player
else if (( move_column == piece_column + 2 && move_row == piece_row + 2 && array[piece_row + 1][piece_column + 1] == otherPlayer ) ||
( move_column == piece_column - 2 && move_row == piece_row + 2 && array[piece_row + 1][piece_column - 1] == otherPlayer ))
{
validMove = 1;
jumpPiece( array, player, otherPlayer, piece_column, piece_row, move_column, move_row );
}
cout << "validPlayerOneMoveCheck = " << validMove << "\n";
return validMove;
}
//player 2 valid move check from top of board (low numbers) to bottom (higher numbers)
bool validPlayerTwoMoveCheck( int array[][10], int player, int piece_column, int piece_row, int move_column, int move_row )
{
bool validMove;
int otherPlayer = otherPlayerDefine( player );
//moving one space to empty space; empty space verified in ValidMoveCheck already
if (( move_column == piece_column + 1 || move_column == piece_column - 1) && ( move_row == piece_row - 1 ))
{
validMove = 1;
movePiece( array, player, otherPlayer, piece_column, piece_row, move_column, move_row );
}
//jumping another player
else if (( move_column == piece_column + 2 && move_row == piece_row - 2 && array[piece_row - 1][piece_column + 1] == otherPlayer ) ||
( move_column == piece_column - 2 && move_row == piece_row - 2 && array[piece_row - 1][piece_column - 1] == otherPlayer ))
{
validMove = 1;
jumpPiece( array, player, otherPlayer, piece_column, piece_row, move_column, move_row );
}
// NEED TO DEAL WITH JUMPING SOMEWHERE!! -
cout << "validPlayerTwoMoveCheck = " << validMove << "\n";
return validMove;
}
void movePiece( int array[][10], int player, int otherPlayer, int piece_column, int piece_row, int move_column, int move_row )
{
//move player piece
cout << "movePiece called \n";
array[piece_row][piece_column] = 0;
array[move_row][move_column] = player;
}
void jumpPiece( int array[][10], int player, int otherPlayer, int piece_column, int piece_row, int move_column, int move_row )
{
cout << "jumpPiece called \n";
cout << "Player " << player << ", you took Player " << otherPlayer << "'s piece!\n";
array[piece_row][piece_column] = 0;
array[move_row][move_column] = player;
array[(move_row + piece_row)/2][(move_column + piece_column)/2] = 0;
}
/*
void checkKing ( int array[][10], int player, int move_column, int move_row )
{
if ( player == 1 && move_row == 10 && array[move_row][move_column] != 3 )
{
array[move_row][move_column] = 3;
cout << "Player 1, you got a king!";
}
else if ( player == 2 && move_row == 1 && array[move_row][move_column] != 4 )
{
array[move_row][move_column] = 4;
cout << "Player 1, you got a king!";
}
}
*/
void displayBoard( int array[10][10] )
{
cout << " 1 2 3 4 5 6 7 8 9 10\n\n";
for ( int i = 1; i < 11; i+=2 )
{
cout << i << " [" << displaySpace( array, i, 1) << "] [" << displaySpace( array, i, 3) << "] [" << displaySpace( array, i, 5) << "] ["
<< displaySpace( array, i, 7) << "] [" << displaySpace( array, i, 9) << "]\n\n";
if ( i < 9 )
{
cout << (i+1) << " [" << displaySpace( array, i+1, 2) << "] [" << displaySpace( array, i+1, 4) << "] [" << displaySpace( array, i+1, 6) <<
"] [" << displaySpace( array, i+1, 8) << "] [" << displaySpace( array, i+1, 10) << "]\n\n";
}
else //this is to account for different spacing with "10" prefix
{
cout << (i+1) << " [" << displaySpace( array, i+1, 2) << "] [" << displaySpace( array, i+1, 4) << "] [" << displaySpace( array, i+1, 6) <<
"] [" << displaySpace( array, i+1, 8) << "] [" << displaySpace( array, i+1, 10) << "]\n";
}
}
}
string displaySpace( int array[10][10], int x, int y )
//this converts integer values to characters to display to the player
{
string display;
switch ( array[x][y])
{
case 0:
display = " ";
break;
case 1:
display = "X";
break;
case 2:
display = "O";
break;
case 3:
display = "Y";
break;
case 4:
display = "M";
break;
case 10:
display = "";
break;
}
return display;
}
int main()
{
int board[10][10];
bool game_over = 0;
int player = 2;
int piece_column;
int piece_row;
int move_column;
int move_row;
bool validPiece;
bool validMove;
//initial piece layout
for ( int i = 1; i < 10; i+=2 )
{
board[1][i] = 1;
board[3][i] = 1;
board[5][i] = 0;
board[7][i] = 2;
board[9][i] = 2;
for ( int j = 2; j < 11; j+=2 )
{
board[j][i] = 10;
}
}
for ( int i = 2; i < 11; i+=2 )
{
board[2][i] = 1;
board[4][i] = 1;
board[6][i] = 0;
board[8][i] = 2;
board[10][i] = 2;
//NO IDEA WHY THE BELOW "FOR" WON'T RUN - COMPILES FINE, BUT WON'T RUN
/* for ( int j = 1; j < 10; j+=2 )
{
board[j][i] = 10;
} */
}
//WHEREAS THIS ONE WORKS FINE
for ( int i = 2; i < 11; i+=2 )
{
for ( int j = 1; j < 10; j+=2 )
{
board[j][i] = 10;
}
}
//WTF??
cout << "Space 2, 1 = " << board[1][2] << "\n";
cout << "Space 6, 10 = " << board[10][6] << "\n";
cout << "Space 10, 10 = " << board[10][10] << "\n";
do
{
//change player
switch ( player )
{
case 1:
player = 2;
break;
case 2:
player = 1;
break;
}
//reset variables
validPiece = 0;
validMove = 0;
//WTF??
cout << "Space 2, 1 = " << board[1][2] << "\n";
cout << "Space 6, 10 = " << board[6][10] << "\n";
cout << "Space 10, 10 = " << board[10][10] << "\n";
do {
do {
cout << "\n\n";
displayBoard( board );
cout << "\n";
cout << "Player " << player << ", make your move.\n";
cout << "Select column of piece to move: ";
cin >> piece_column;
cout << "\nSelect row of piece to move: ";
cin >> piece_row;
validPiece = validPieceCheck( board, player, piece_column, piece_row );
} while ( validPiece == 0 );
validPiece = 0;
cout << "\nSelect column to move to: ";
cin >> move_column;
cout << "\nSelect row to move to: ";
cin >> move_row;
cout << "\n";
validMove = validMoveCheck( board, player, piece_column, piece_row, move_column, move_row );
} while ( validMove == 0 );
} while ( game_over == 0 );
答案 0 :(得分:0)
谢谢,AnotherTest。我只是意识到 - 我没有从零开始计算数组的元素,我数着1-10。 :/