带随机硬币翻转的井字游戏
Tic Tac Toe with Random coin flip
来自 class 的家庭作业问题:我必须生成一个井字游戏,该游戏使用掷硬币来确定谁先走。我已经生成了我的大部分代码,但仍有几个 things/bugs 我无法理解。
- 我会抛硬币。但是,如何让程序选择谁先走。
- 获胜方格或游戏结束方格上没有 X 或 O。
- 平局没有消息。
不输入任何值直接按回车没有消息。
#include "pch.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
void Flip_Coin();
void Do_Exercise();
void Display_Board();
void Ask_Turn();
char Check_Winner();
void Computer_Player_Turn();
char Board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int n = 0;
int main()
{
srand(time(0));
Do_Exercise();
cin.get();
return 0;
}
void Do_Exercise()
{
Flip_Coin();
while (true)
{
n++;
Display_Board();
Ask_Turn();
if (Check_Winner() == 'X')
{
cout << "Human Wins!" << endl;
break;
}
else if (Check_Winner() == 'O')
{
cout << "Computer Wins!" << endl;
break;
}
else if (Check_Winner() == 'T' && n == 9)
{
cout << "It's a draw." << endl;
break;
}
Computer_Player_Turn();
if (Check_Winner() == 'X')
{
cout << "Human Wins!" << endl;
break;
}
else if (Check_Winner() == 'O')
{
cout << "Computer Wins!" << endl;
break;
}
else if (Check_Winner() == 'T' && n == 9)
{
cout << "It's a draw." << endl;
break;
}
}
}
void Flip_Coin()
{
int Flip;
cout << "Welcome to Tic - Tac - toe.\n\n"
<< "Wait I am flipping a coin to see who goes first . . .\n\n";
Flip = rand() % 2;
if (Flip == 0)
{
cout << "Computer wins coin toss.\n\n";
}
else
{
cout << "Human wins coin toss.\n\n";
}
cout << "The board is laid out like this:\n\n";
}
void Display_Board()
{
for (int Row = 0; Row < 3; Row++)
{
for (int Column = 0; Column < 3; Column++)
{
cout << Board[Row][Column] << "\t";
}
cout << "\n\n";
}
}
void Ask_Turn()
{
string input;
while (true)
{
cout << "Enter the position to place your X: ";
cin >> input;
cout << endl;
if (input != "")
{
char entered = input.c_str()[0]; // sets char character to act in place of string
if (entered >= '1' && entered <= '9')
{
cout << "The human places a X-token at position: " << entered << "\n\n";
cout << "Current board:\n\n";
int entered_number = entered - '0'; // changes char to int form
int index = entered_number - 1;
int row = index / 3;
int col = index % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
cout << "That position is already taken. ";
}
else {
Board[row][col] = 'X';
break;
}
}
else {
cout << "You must entered in the the range of 1-9.\n";
}
}
else {
cout << "You must enter something!"; // doesnt work
}
}
}
char Check_Winner()
{
//first player
if (Board[0][0] == 'X' && Board[0][1] == 'X' && Board[0][2] == 'X')
return 'X';
if (Board[1][0] == 'X' && Board[1][1] == 'X' && Board[1][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[2][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][0] == 'X' && Board[2][0] == 'X')
return 'X';
if (Board[0][1] == 'X' && Board[1][1] == 'X' && Board[2][1] == 'X')
return 'X';
if (Board[0][2] == 'X' && Board[1][2] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[1][1] == 'X' && Board[0][2] == 'X')
return 'X';
//second player
if (Board[0][0] == 'O' && Board[0][1] == 'O' && Board[0][2] == 'O')
return 'O';
if (Board[1][0] == 'O' && Board[1][1] == 'O' && Board[1][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[2][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][0] == 'O' && Board[2][0] == 'O')
return 'O';
if (Board[0][1] == 'O' && Board[1][1] == 'O' && Board[2][1] == 'O')
return 'O';
if (Board[0][2] == 'O' && Board[1][2] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[1][1] == 'O' && Board[0][2] == 'O')
return 'O';
return 'T';
}
void Computer_Player_Turn()
{
while (true)
{
int computer_choice = (rand() % 9) + 1;
int row = (computer_choice - 1) / 3;
int col = (computer_choice - 1) % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
continue;
}
else {
cout << "The Computer places a O-token at position: " << computer_choice << "\n\n";
Board[row][col] = 'O';
break;
}
}
}
这是解决方案。
1) 现在您已经硬编码为先调用 Ask_Turn
然后再调用 Computer_Player_Turn
。不要这样调用它们,而是先从抛硬币调用它们,然后让两个函数在完成时相互调用。
2) 在检查获胜者之前致电 Display_Board()
。
3) 已修复。您每 2 回合只将 n 加 1。
4) 在while循环中求输入,只要输入为空就在循环中
因此,进行所有这些更改后,代码应类似于以下内容。
我已经测试了这里的代码,https://onlinegdb.com/S1Qdcr63X
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
void Flip_Coin();
void Do_Exercise();
void Display_Board();
void Ask_Turn();
char Check_Winner();
void Computer_Player_Turn();
char Board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int n = 0;
void Computer_Player_Turn();
void Ask_Turn();
int main()
{
srand(time(0));
Do_Exercise();
cin.get();
return 0;
}
void Do_Exercise()
{
Flip_Coin();
}
void Flip_Coin()
{
int Flip;
cout << "Welcome to Tic - Tac - toe.\n\n"
<< "Wait I am flipping a coin to see who goes first . . .\n\n";
Flip = rand() % 2;
if (Flip == 0)
{
cout << "Computer wins coin toss.\n\n";
Computer_Player_Turn();
}
else
{
cout << "Human wins coin toss.\n\n";
Ask_Turn();
}
}
void Display_Board()
{
for (int Row = 0; Row < 3; Row++)
{
for (int Column = 0; Column < 3; Column++)
{
cout << Board[Row][Column] << "\t";
}
cout << "\n\n";
}
}
char Check_Winner()
{
//first player
if (Board[0][0] == 'X' && Board[0][1] == 'X' && Board[0][2] == 'X')
return 'X';
if (Board[1][0] == 'X' && Board[1][1] == 'X' && Board[1][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[2][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][0] == 'X' && Board[2][0] == 'X')
return 'X';
if (Board[0][1] == 'X' && Board[1][1] == 'X' && Board[2][1] == 'X')
return 'X';
if (Board[0][2] == 'X' && Board[1][2] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[1][1] == 'X' && Board[0][2] == 'X')
return 'X';
//second player
if (Board[0][0] == 'O' && Board[0][1] == 'O' && Board[0][2] == 'O')
return 'O';
if (Board[1][0] == 'O' && Board[1][1] == 'O' && Board[1][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[2][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][0] == 'O' && Board[2][0] == 'O')
return 'O';
if (Board[0][1] == 'O' && Board[1][1] == 'O' && Board[2][1] == 'O')
return 'O';
if (Board[0][2] == 'O' && Board[1][2] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[1][1] == 'O' && Board[0][2] == 'O')
return 'O';
return 'T';
}
bool winner()
{
if (Check_Winner() == 'X')
{
cout << "Human Wins!" << endl;
return true;
}
else if (Check_Winner() == 'O')
{
cout << "Computer Wins!" << endl;
return true;
}
else if (Check_Winner() == 'T' && n == 9)
{
cout << "It's a draw." << endl;
return true;
}
return false;
}
void Ask_Turn()
{
n++;
string input = "";
while (true)
{
do
{
cout << "Enter the position to place your X: ";
cin >> input;
cout << endl;
}
while(input == "" || (input.c_str()[0] < '1' || input.c_str()[0] > '9'));
char entered = input.c_str()[0]; // sets char character to act in place of string
cout << "The human places a X-token at position: " << entered << "\n\n";
cout << "Current board:\n\n";
int entered_number = entered - '0'; // changes char to int form
int index = entered_number - 1;
int row = index / 3;
int col = index % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
cout << "That position is already taken. ";
n--;
Ask_Turn();
}
else {
Board[row][col] = 'X';
break;
}
}
Display_Board();
if(winner())
return;
else
Computer_Player_Turn();
}
void Computer_Player_Turn()
{
n++;
while (true)
{
int computer_choice = (rand() % 9) + 1;
int row = (computer_choice - 1) / 3;
int col = (computer_choice - 1) % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
continue;
}
else {
cout << "The Computer places a O-token at position: " << computer_choice << "\n\n";
Board[row][col] = 'O';
break;
}
}
Display_Board();
if(winner())
return;
else
Ask_Turn();
}
来自 class 的家庭作业问题:我必须生成一个井字游戏,该游戏使用掷硬币来确定谁先走。我已经生成了我的大部分代码,但仍有几个 things/bugs 我无法理解。
- 我会抛硬币。但是,如何让程序选择谁先走。
- 获胜方格或游戏结束方格上没有 X 或 O。
- 平局没有消息。
不输入任何值直接按回车没有消息。
#include "pch.h" #include <iostream> #include <string> #include <cstdlib> #include <time.h> using namespace std; void Flip_Coin(); void Do_Exercise(); void Display_Board(); void Ask_Turn(); char Check_Winner(); void Computer_Player_Turn(); char Board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; int n = 0; int main() { srand(time(0)); Do_Exercise(); cin.get(); return 0; } void Do_Exercise() { Flip_Coin(); while (true) { n++; Display_Board(); Ask_Turn(); if (Check_Winner() == 'X') { cout << "Human Wins!" << endl; break; } else if (Check_Winner() == 'O') { cout << "Computer Wins!" << endl; break; } else if (Check_Winner() == 'T' && n == 9) { cout << "It's a draw." << endl; break; } Computer_Player_Turn(); if (Check_Winner() == 'X') { cout << "Human Wins!" << endl; break; } else if (Check_Winner() == 'O') { cout << "Computer Wins!" << endl; break; } else if (Check_Winner() == 'T' && n == 9) { cout << "It's a draw." << endl; break; } } } void Flip_Coin() { int Flip; cout << "Welcome to Tic - Tac - toe.\n\n" << "Wait I am flipping a coin to see who goes first . . .\n\n"; Flip = rand() % 2; if (Flip == 0) { cout << "Computer wins coin toss.\n\n"; } else { cout << "Human wins coin toss.\n\n"; } cout << "The board is laid out like this:\n\n"; } void Display_Board() { for (int Row = 0; Row < 3; Row++) { for (int Column = 0; Column < 3; Column++) { cout << Board[Row][Column] << "\t"; } cout << "\n\n"; } } void Ask_Turn() { string input; while (true) { cout << "Enter the position to place your X: "; cin >> input; cout << endl; if (input != "") { char entered = input.c_str()[0]; // sets char character to act in place of string if (entered >= '1' && entered <= '9') { cout << "The human places a X-token at position: " << entered << "\n\n"; cout << "Current board:\n\n"; int entered_number = entered - '0'; // changes char to int form int index = entered_number - 1; int row = index / 3; int col = index % 3; char grid_position = Board[row][col]; if (grid_position == 'X' || grid_position == 'O') { cout << "That position is already taken. "; } else { Board[row][col] = 'X'; break; } } else { cout << "You must entered in the the range of 1-9.\n"; } } else { cout << "You must enter something!"; // doesnt work } } } char Check_Winner() { //first player if (Board[0][0] == 'X' && Board[0][1] == 'X' && Board[0][2] == 'X') return 'X'; if (Board[1][0] == 'X' && Board[1][1] == 'X' && Board[1][2] == 'X') return 'X'; if (Board[2][0] == 'X' && Board[2][1] == 'X' && Board[2][2] == 'X') return 'X'; if (Board[0][0] == 'X' && Board[1][0] == 'X' && Board[2][0] == 'X') return 'X'; if (Board[0][1] == 'X' && Board[1][1] == 'X' && Board[2][1] == 'X') return 'X'; if (Board[0][2] == 'X' && Board[1][2] == 'X' && Board[2][2] == 'X') return 'X'; if (Board[0][0] == 'X' && Board[1][1] == 'X' && Board[2][2] == 'X') return 'X'; if (Board[2][0] == 'X' && Board[1][1] == 'X' && Board[0][2] == 'X') return 'X'; //second player if (Board[0][0] == 'O' && Board[0][1] == 'O' && Board[0][2] == 'O') return 'O'; if (Board[1][0] == 'O' && Board[1][1] == 'O' && Board[1][2] == 'O') return 'O'; if (Board[2][0] == 'O' && Board[2][1] == 'O' && Board[2][2] == 'O') return 'O'; if (Board[0][0] == 'O' && Board[1][0] == 'O' && Board[2][0] == 'O') return 'O'; if (Board[0][1] == 'O' && Board[1][1] == 'O' && Board[2][1] == 'O') return 'O'; if (Board[0][2] == 'O' && Board[1][2] == 'O' && Board[2][2] == 'O') return 'O'; if (Board[0][0] == 'O' && Board[1][1] == 'O' && Board[2][2] == 'O') return 'O'; if (Board[2][0] == 'O' && Board[1][1] == 'O' && Board[0][2] == 'O') return 'O'; return 'T'; } void Computer_Player_Turn() { while (true) { int computer_choice = (rand() % 9) + 1; int row = (computer_choice - 1) / 3; int col = (computer_choice - 1) % 3; char grid_position = Board[row][col]; if (grid_position == 'X' || grid_position == 'O') { continue; } else { cout << "The Computer places a O-token at position: " << computer_choice << "\n\n"; Board[row][col] = 'O'; break; } } }
这是解决方案。
1) 现在您已经硬编码为先调用 Ask_Turn
然后再调用 Computer_Player_Turn
。不要这样调用它们,而是先从抛硬币调用它们,然后让两个函数在完成时相互调用。
2) 在检查获胜者之前致电 Display_Board()
。
3) 已修复。您每 2 回合只将 n 加 1。
4) 在while循环中求输入,只要输入为空就在循环中
因此,进行所有这些更改后,代码应类似于以下内容。
我已经测试了这里的代码,https://onlinegdb.com/S1Qdcr63X
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
void Flip_Coin();
void Do_Exercise();
void Display_Board();
void Ask_Turn();
char Check_Winner();
void Computer_Player_Turn();
char Board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int n = 0;
void Computer_Player_Turn();
void Ask_Turn();
int main()
{
srand(time(0));
Do_Exercise();
cin.get();
return 0;
}
void Do_Exercise()
{
Flip_Coin();
}
void Flip_Coin()
{
int Flip;
cout << "Welcome to Tic - Tac - toe.\n\n"
<< "Wait I am flipping a coin to see who goes first . . .\n\n";
Flip = rand() % 2;
if (Flip == 0)
{
cout << "Computer wins coin toss.\n\n";
Computer_Player_Turn();
}
else
{
cout << "Human wins coin toss.\n\n";
Ask_Turn();
}
}
void Display_Board()
{
for (int Row = 0; Row < 3; Row++)
{
for (int Column = 0; Column < 3; Column++)
{
cout << Board[Row][Column] << "\t";
}
cout << "\n\n";
}
}
char Check_Winner()
{
//first player
if (Board[0][0] == 'X' && Board[0][1] == 'X' && Board[0][2] == 'X')
return 'X';
if (Board[1][0] == 'X' && Board[1][1] == 'X' && Board[1][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[2][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][0] == 'X' && Board[2][0] == 'X')
return 'X';
if (Board[0][1] == 'X' && Board[1][1] == 'X' && Board[2][1] == 'X')
return 'X';
if (Board[0][2] == 'X' && Board[1][2] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[1][1] == 'X' && Board[0][2] == 'X')
return 'X';
//second player
if (Board[0][0] == 'O' && Board[0][1] == 'O' && Board[0][2] == 'O')
return 'O';
if (Board[1][0] == 'O' && Board[1][1] == 'O' && Board[1][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[2][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][0] == 'O' && Board[2][0] == 'O')
return 'O';
if (Board[0][1] == 'O' && Board[1][1] == 'O' && Board[2][1] == 'O')
return 'O';
if (Board[0][2] == 'O' && Board[1][2] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[1][1] == 'O' && Board[0][2] == 'O')
return 'O';
return 'T';
}
bool winner()
{
if (Check_Winner() == 'X')
{
cout << "Human Wins!" << endl;
return true;
}
else if (Check_Winner() == 'O')
{
cout << "Computer Wins!" << endl;
return true;
}
else if (Check_Winner() == 'T' && n == 9)
{
cout << "It's a draw." << endl;
return true;
}
return false;
}
void Ask_Turn()
{
n++;
string input = "";
while (true)
{
do
{
cout << "Enter the position to place your X: ";
cin >> input;
cout << endl;
}
while(input == "" || (input.c_str()[0] < '1' || input.c_str()[0] > '9'));
char entered = input.c_str()[0]; // sets char character to act in place of string
cout << "The human places a X-token at position: " << entered << "\n\n";
cout << "Current board:\n\n";
int entered_number = entered - '0'; // changes char to int form
int index = entered_number - 1;
int row = index / 3;
int col = index % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
cout << "That position is already taken. ";
n--;
Ask_Turn();
}
else {
Board[row][col] = 'X';
break;
}
}
Display_Board();
if(winner())
return;
else
Computer_Player_Turn();
}
void Computer_Player_Turn()
{
n++;
while (true)
{
int computer_choice = (rand() % 9) + 1;
int row = (computer_choice - 1) / 3;
int col = (computer_choice - 1) % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
continue;
}
else {
cout << "The Computer places a O-token at position: " << computer_choice << "\n\n";
Board[row][col] = 'O';
break;
}
}
Display_Board();
if(winner())
return;
else
Ask_Turn();
}