[英]Making a function for a tie game in Tic Tac Toe
I am making a tic tac toe game, and so far there is complete functionality except for a tie game instance. 我正在制作一个tic tac toe游戏,到目前为止,除了领带游戏实例外,还有完整的功能。 The program doesnt end when there isnt a winner.
当没有胜利者时,该计划不会结束。 I am not sure how to approach this, maybe I should add to my champion function, or make a completely new function.
我不知道如何处理这个,也许我应该添加到我的冠军功能,或者做一个全新的功能。 Some suggestions would be great.
一些建议会很棒。 Im a beginner so any criticism is greatly appreciated.
我是初学者,所以任何批评都非常感谢。
#define GRID_SIZE 3
class TicTacToe {
private:
char map[GRID_SIZE][GRID_SIZE];
public:
void computers_turn() {
while (true) {
int choice = (rand() % 9) + 1;
int row = choice / 3;
int col = choice % 3;
char grid_position = map[row][col];
if (grid_position == 'X' || grid_position == 'O') {
std::cout << "Space taken. Try again" << std::endl;
} else {
map[row][col] = (char) 'O';
break;
}
}
}
// determines any winning combination, horizontal, diagonal,
//vertical
void champion() {
const char *possiblities[8]{
"123",
"456",
"789",
"147",
"159",
"258",
"369",
"753"
};
for (int i = 0; i < 8; i++) {
char previous_pos = '0';
const char *possible_moves = possiblities[i];
bool winner = true;
for (int index = 0; index < GRID_SIZE; index++) {
char character = possible_moves[index];
int entered_num = character - '0';
int grid_space = entered_num - 1;
int row = grid_space / GRID_SIZE;
int col = grid_space % GRID_SIZE;
char grid_coordinate = map[row][col];
if (previous_pos == '0') {
previous_pos = grid_coordinate;
} else if
(previous_pos == grid_coordinate) {
continue;
} else {
winner = false;
break;
}
}
if (winner) {
puts("You win");
exit(0);
}
}
}
// initializes game, asks user for input
void playgame() {
std::string input;
while (true) {
std::cout << "Go player one" << std::endl;
getline(std::cin, input);
if (input != " ") {
char entered = input.c_str()[0];
if (entered >= '1' && entered <= '9') {
int entered_num = entered - '0';
int index = entered_num - 1;
int row = index / 3;
int col = index % 3;
char grid_position = map[row][col];
if (grid_position == 'X' || grid_position == 'O') {
std::cout << "Space taken. Try again" << std::endl;
} else {
map[row][col] = (char) 'X';
break;
}
} else {
std::cout << "Only numbers 1 - 9" << std::endl;
}
} else {
std::cout << "Have to enter something, try again" << std::endl;
}
}
}
// generates tic tac toe grid
void generateGrid() {
int number = 1;
for (int x = 0; x < GRID_SIZE; x++) {
for (int y = 0; y < GRID_SIZE; y++) {
map[x][y] = std::to_string(number).c_str()[0];
number += 1;
}
}
}
// displays updated tic tac toe grid
void tictacToeMap() {
std::cout << std::endl;
for (int x = 0; x < GRID_SIZE; x++) {
for (int y = 0; y < GRID_SIZE; y++) {
std::printf(" %c ", map[x][y]);
}
std::cout << std::endl;
}
}
TicTacToe() {
generateGrid();
while (true) {
champion();
tictacToeMap();
playgame();
computers_turn();
}
}
};
int main() {
TicTacToe tic;
tic.playgame();
return 0;
You could initialize all the spaces in the grid to ''
, then after each turn you could call a function to check if the game is a tie. 你可以将网格中的所有空格初始化为
''
,然后在每次转弯后你可以调用一个函数来检查游戏是否是平局。 The function could have a for loop inside of a for loop that just checks if all the grids have a value not equal to ''
该函数可以在for循环内部有一个for循环,只检查所有网格是否具有不等于
''
的值
Here is a sample function: 这是一个示例函数:
bool IsGameOver(char map[][])
{
bool answer = true;
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
char grid_pos = map[i][j];
if (grid_pos=='')
answer = false;
}
}
return answer;
}
Just implement something like this 只需实现这样的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.