上周我做了一个作业,我们必须使用 3*3 棋盘编写井字游戏。现在,我正在尝试自己做同样的游戏,但玩家可以自定义棋盘大小。
我询问用户希望的行数和列数,我正确地创建了游戏板并使用我的 << 重载函数打印了这个游戏板。一切似乎都还好。但是当我尝试做一个动作(在棋盘上放置一个标记)时,用户传递的 x 和 y 坐标与我们在屏幕上看到的不匹配。
我不知道错误是来自我的 SetPosition() 还是 AtPosition()。这是我第一次处理这种数组,所以希望有人能帮助我。(对不起,我的英语不好)
那里有我的驱动程序文件和我的 GameBoard.h 和 .cpp 如果您需要更多信息,请询问我。
#include "GameBoard.h"
#include <iostream> /*cout, cin*/
#include <stdlib.h> /* rand, srand */
#include <time.h> /* time */
using namespace std;
// ************Prototypes*************
int RandomInt(int low, int high); // generate a random number
// ***********************************
int main(void)
{
// *************Variables************
int x_size; // the user decide of the size of the board
int y_size; // the user decide of the size of the board
int x_player; // the user x-coordinate
int y_player; // the user y-coordinate
int ID_player = 1; // player ID
int x_computer; // the computer x-coordinate
int y_computer; // the computer y-coordinate
int ID_computer = 2; // computer ID
int keepplaying = 0; // use this for game loop until we do not have the check win function
// **********************************
srand(time(0));
// ask to the user which size he wants to use
cout << "Welcome to my Tic Tae Toe Game!" << endl;
cout << "What size do you want?" << endl;
cout << "Insert number of rows" << endl;
cin >> x_size;
cout << "Insert number of columns" << endl;
cin >> y_size;
cout << "This is the GameBoard we are using" << endl;
// create the GameBoard
Board myBoard(x_size, y_size);
// set all the array to 0
myBoard.ResetBoard();
// prints the 2D array
cout << myBoard << endl;
cout << "You are the player 1 and you play against the computer (number 2). Good Luck!" << endl;
do
{
// ask to the user the coordinates in which he wants to place the mark
do
{
cout << "Player 1 turn! " << endl;
cout << "Insert x coordinate: ";
cin >> x_player ;
cout << "Insert y coordinate: ";
cin >> y_player ;
}
while( (x_player < 0 || x_player > x_size) || (y_player < 0 || y_player > y_size) || myBoard.AtPosition(x_player, y_player) != 0 );
// while the cell is not available or the coordinates are not valide
myBoard.SetPosition(x_player, y_player , ID_player); // the mark has been placed
// print the move
cout << myBoard << endl;
cout << "End of turn!" << endl;
// The computer place a mark: the x and y coordinates are randomly generated
cout << "Computer Turn:" << endl;
do
{
x_computer = RandomInt(0,x_size);
y_computer = RandomInt(0,y_size);
}while(myBoard.AtPosition(x_computer, y_computer) != 0);
myBoard.SetPosition(x_computer, y_computer, ID_computer);
cout << myBoard << endl;
}while(keepplaying != 1) ;
//return 0;
}
// This function create a random number
int RandomInt(int low, int high)
{
int number = rand() % (high - low + 1) + low;
return number;
}
这是 main.cpp,它会询问用户的数据,然后调用在我的类 Board 中声明和定义的函数
#include <iostream> /*cout, cin, ostream*/
class Board
{
public:
/* Board()
{
x = 3;
y = 3;
//player_ID = 1;
//computer_ID = 2;
}*/
Board(int x_, int y_); // non-default constructor
void SetPosition(int x, int y, int player); // Place a mark
int AtPosition(int x, int y); // return the ID of the cell
void ResetBoard(void); // set all the array to be 0
friend std::ostream& operator<<(std::ostream &os, const Board &rhs); // overload the << operator
private:
int x; // x-coordinates
int y; // y-coordinates
int *board; // contains the board in a 2D array (3*3 gameboard)
int player_ID; // player ID
int computer_ID; // computer ID
};
你有我班级的头文件
#include "GameBoard.h"
// non-default constructor
Board::Board(int x_, int y_)
{
x = x_;
y = y_;
board = new int[x*y];
}
// Goes through the board array and set all the ID to 0
void Board::ResetBoard()
{
for(int i = 0; i < x*y; i++)
{
board[i] = 0;
}
}
// returns the ID of the passed cell
int Board::AtPosition(int x, int y)
{
return board[x*y];
}
// put a mark in the cell and return true
void Board::SetPosition(int x, int y, int player_ID)
{
board[x*y +x] = player_ID;
//value = pd[row * 4 + column];
}
std::ostream& operator<<(std::ostream &os, const Board &rhs)
{
for (int i = 0; i < rhs.x*rhs.y; i++)
{
if(i % rhs.y == 0)
{
os << std::endl;
}
os << rhs.board[i];
}
return os;
}
我希望一切都清楚并得到很好的评论