You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<iostream>
#include<windows.h>
char arr[9] = { ' ',' ',' ',' ',' ',' ',' ',' ',' ' };
void print()
{
std::cout << "井字棋游戏" << std::endl;
std::cout << "+---+---+---+" << std::endl;
std::cout << "| " << arr[0] << " | " << arr[1] << " | " << arr[2] << " |" << std::endl;
std::cout << "+---+---+---+" << std::endl;
std::cout << "| " << arr[3] << " | " << arr[4] << " | " << arr[5] << " |" << std::endl;
std::cout << "+---+---+---+" << std::endl;
std::cout << "| " << arr[6] << " | " << arr[7] << " | " << arr[8] << " |" << std::endl;
std::cout << "+---+---+---+" << std::endl;
}
int main()
{
bool n1{ 0 }, n2{ 0 };
system("cls");
print();
int i{ 1 }, k{};
unsigned long long n;//落棋位置
int a{ 0 }, b{ 0 };//赢局对比
int round{};
std::cout << "请输入想进行的游戏局数:";
std::cin >> round;
for (k = 1; k <= round; ++k)
{
for (i = 1; i <= 9; i = i + 1)
{
if (i % 2 == 1)
{
std::cout << "请玩家一选择落棋位置:";
std::cin >> n;
if (arr[n - 1] != ' '||n>9||n<1)
{
std::cout << "落棋位置错误" << std::endl;
std::cout << "请玩家重新选择落棋位置" << std::endl;
i = i - 1;
continue;
}
else
{
arr[n - 1] = 'X';
}
}
else
{
std::cout << "请玩家二选择落棋位置:";
std::cin >> n;
if (arr[n - 1] != ' '||n>9||n<1)
{
std::cout << "落棋位置错误" << std::endl;
std::cout << "请玩家重新选择落棋位置" << std::endl;
i = i - 1;
continue;
}
else
{
arr[n - 1] = 'O';
}
}
system("cls");
print();
//横
if (arr[0] == arr[1] && arr[1] == arr[2] && arr[2] != ' ')
{
if (arr[0] == 'O')
n2 = 1;
else
n1 = 1;
break;
}
else if (arr[3] == arr[4] && arr[4] == arr[5] && arr[5] != ' ')
{
if (arr[3] == 'O')
n2 = 1;
else
n1 = 1;
break;
}
else if (arr[6] == arr[7] && arr[7] == arr[8] && arr[8] != ' ')
{
if (arr[6] == 'O')
n2 = 1;
else
n1 = 1;
break;
}
else if (arr[0] == arr[3] && arr[3] == arr[6] && arr[6] != ' ')
{
if (arr[0] == 'O')
n2 = 1;
else
n1 = 1;
break;
}
else if (arr[1] == arr[4] && arr[4] == arr[7] && arr[7] != ' ')
{
if (arr[1] == 'O')
n2 = 1;
else
n1 = 1;
break;
}
else if (arr[2] == arr[5] && arr[5] == arr[8] && arr[8] != ' ')
{
if (arr[2] == 'O')
n2 = 1;
else
n1 = 1;
break;
}
//斜
else if (arr[0] == arr[4] && arr[4] == arr[8] && arr[8] != ' ')
{
if (arr[0] == 'O')
n2 = 1;
else
n1 = 1;
break;
}
else if (arr[6] == arr[4] && arr[4] == arr[2] && arr[2] != ' ')
{
if (arr[6] == 'O')
n2 = 1;
else
n1 = 1;
break;
}
}
//单局判断
if (n1 == 1)
++a;
else if (n2 == 1)
++b;
std::cout << "比分对比 玩家1玩家2=" << a << ':' << b << std::endl;
n1 = 0, n2 = 0;
for (i = 0; i < 9; ++i)
arr[i] = ' ';
}
//最终判断
if (a>b)
std::cout << "玩家1 win!!!";
else if (a<b)
std::cout << "玩家2 win!!!";
else
std::cout << "no winner!!!";
}