C TIC-TAC-TOE AI不起作用
C++ Tic-Tac-Toe AI not working
这是我第一次在这里提出问题。如果我在这里犯了任何错误,请不要太生气,然后让我知道该修复什么。非常感谢:D
我正在尝试编写一个C 代码,您可以对CPU进行TIC-TAC-TOE。CPU必须赢或至少领带。CPU首先。作为播放器,将要求您输入2个编号,每个号码用于行和列。
问题是,当我执行程序时,它在第32行开始的第三个循环中没有任何错误消息停止。我想知道我做错了什么。
#include <iostream>
using namespace std;
void displayBoard(char []); //displays current board
bool isValidInput(char [], int, int, int); //checks the user input
bool isItAScore(char [], char); //determines if current player won or not
int getValue(char [], char, int); //get value of the board by player and number marks
int main()
{
char tBoard[9], computer = 'X', player = 'O', empty = '.';
int board[3][3], row = -1, col = -1, temp = 0;
bool didSomeoneWin = false;
//initializes the board with numbers 0 to 8
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = temp;
temp++;
}
}
//initialize the actual board with '.'
for(int i= 0; i < 9; i++)
{
tBoard[i] = empty;
}
//starts the game
for(int k = 0; k < 9; k++) //it lasts only 9 turns max
{
displayBoard(tBoard);
if(k % 2 == 1) //player's turn
{
cout << "Player, row and column: ";
cin >> row >> col; //takes user input range of 1 to 3
//decreases each value by 1 to work with the array index 0 ~ 2
row--;
col--;
if(isValidInput(tBoard, row, col, board[row][col])) //iff the input is valid
{
tBoard[board[row][col]] = player; //puts the mark on the position
if(k > 4 && isItAScore(tBoard, player)) //iff the current player wins
{
displayBoard(tBoard);
cout << "Player wins.";
k = 8; //skips to the end of the loop
didSomeoneWin = true; //no tie
}
}
else //if the input is invalid
{
cout << "Invalid row or column number. Try again.n";
k--; //redo the loop with same player
}
}
else //computer's turn
{
cout << "Computer's moven";
if(k == 0) //first move
{
tBoard[5] = computer;
}
else
{
int value = -100, int position = -1;
for(int i = 0; i < 9; i++)
{
if(tBoard[i] == empty)
{
tBoard[i] = computer;
if(isItAScore(tBoard, computer))
{
displayBoard(tBoard);
cout << "Computer wins.";
i = 8;
k = 8;
didSomeoneWin = true;
}
else
{
int x1 = getValue(tBoard, computer, 1);
int x2 = getValue(tBoard, computer, 2);
int o1 = getValue(tBoard, player, 1);
int o2 = getValue(tBoard, player, 2);
if(value < 3 * x2 + x1 - (3 * o2 + o1))
{
value = 3 * x2 + x1 - (3 * o2 + o1);
position = i;
}
}
if(!didSomeoneWin)
{
tBoard[i] = empty;
}
}
}
tBoard[position] = computer;
}
}
}
if(!didSomeoneWin) //in case of tie
{
displayBoard(tBoard);
cout << "The cat wins";
}
return 0;
}
//display the given board
void displayBoard(char brd[])
{
for(int i = 0; i < 9; i++)
{
cout << brd[i] << " ";
if((i + 1) % 3 == 0)
{
cout << endl;
}
}
}
//checks the input
bool isValidInput(char brd[], int i, int j, int k)
{
if(((i >= 0 && i <= 2) && (j >= 0 && j <= 2)) && brd[k] == '.') //number between 0 and 2, and not taken by any players
{
return true;
}
else
{
return false;
}
}
//checks if the given player or computer won or not
bool isItAScore(char brd[], char c)
{
//chekcs rows
if((brd[0] == c && brd[1] == c && brd[2] == c) || (brd[3] == c && brd[4] == c && brd[5] == c) || (brd[6] == c && brd[7] == c && brd[8] == c))
{
return true;
}
else
{
//checks columns
if((brd[0] == c && brd[3] == c && brd[6] == c) || (brd[1] == c && brd[4] == c && brd[7] == c) || (brd[2] == c && brd[5] == c && brd[8] == c))
{
return true;
}
else
{
//checks diagonals
if((brd[0] == c && brd[4] == c && brd[8] == c) || (brd[2] == c && brd[4] == c && brd[6] == c))
{
return true;
}
//if none of them fails
else
{
return false;
}
}
}
}
int getValue(char brd[], char c, int n)
{
int temp = 0, mark = 0;
bool eligible = true;
for(int i = 0; i < 9; i + 3) //check rows
{
for(int j = i; j < i + 3; j++)
{
if(brd[j] != c)
{
j = 10;
eligible = false;
}
else
{
if(brd[j] == c)
{
temp++;
}
}
}
if(eligible && temp == n)
{
mark++;
}
eligible = true;
temp = 0;
}
for(int i = 0; i < 3; i++) //check columes
{
for(int j = i; j < i + 7; j + 3)
{
if(brd[j] != c)
{
j = 10;
eligible = false;
}
else
{
if(brd[j] == c)
{
temp++;
}
}
}
if(eligible && temp == n)
{
mark++;
}
eligible = true;
temp = 0;
}
for(int i = 0; i < 9; i + 4) //check '' diagonal
{
if(brd[i] != c)
{
i = 10;
eligible = false;
}
else
{
if(brd[i] == c)
{
temp++;
}
}
}
if(eligible && temp == n)
{
mark++;
}
eligible = true;
temp = 0;
for(int i = 2; i < 9; i + 2) //check '/' diagonal
{
if(brd[i] != c)
{
i = 10;
eligible = false;
}
else
{
if(brd[i] == c)
{
temp++;
}
}
}
if(eligible && temp == n)
{
mark++;
}
eligible = true;
temp = 0;
return mark;
}
我尝试使用称为minimax(公式)的东西。我希望它能按预期正常工作。它应该对抗人并取得最佳成绩(获胜或领带,不会输)。
它只是停止而没有任何错误消息。以下链接是在线编译器,我正在使用:
http://cpp.sh/
我发现了很多不寻常的东西。您第一次尝试CPP?代码中的首先是一个错误:
int value = -100, int position = -1;
这应该是
int value = -100;
int position = -1;
或
int value = -100, position = -1;
计算机的第一步将令牌设置为中间的右侧。我认为您喜欢将其设置为中间位置
tBoard[5] = computer;// should be tBorad[4]
下一个问题,您有一个无尽的循环:
for(int i = 0; i < 9; i + 3) // right would be i+=3
我永远不会更改的变量,因为您只阅读它,并且不编写它。这发生了5次。带有启用警告的编译器将有助于找到这样的错误。
接下来,使用 break 和继续,而不是一些疯狂的环路语句。而不是这个,
for(int i = 2; i < 9; i += 2) //check '/' diagonal
{
if(brd[i] != c)
{
i = 10;
eligible = false;
}
}
使用 break :
for(int i = 2; i < 9; i += 2) //check '/' diagonal
{
if(brd[i] != c)
{
eligible = false;
break;
}
}
所有这些错误都消失了,该程序似乎可以正常工作。我认为您应该使用真正的编译器,例如GCC。使用GCC,您可以使用标志查看警告。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- C TIC-TAC-TOE AI不起作用
- A类{..};typedef a a_t;Union u{...; a_t ai;...};不起作用