这是以前整的一个
代码。
制作不容易
给的赞吧......
样例输入:
A1
A2
A3
B1
B2
B3
C1
C2
C3
这样输入:不要有空格!!
上代码
#include <iostream>
#include <string>
#include <cstddef>
#include <stdexcept>
using namespace std;
char point_now[3][3] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
char player = 'X';
unsigned step_count = 0;
int win_lose(char point[3][3], int n)
{
if (point[0][0] == point[0][1] && point[0][1] == point[0][2] && point[0][0] == 'X') return 1; // X wins
if (point[1][0] == point[1][1] && point[1][1] == point[1][2] && point[1][0] == 'X') return 1; // X wins
if (point[2][0] == point[2][1] && point[2][1] == point[2][2] && point[2][0] == 'X') return 1; // X wins
if (point[0][0] == point[1][0] && point[1][0] == point[2][0] && point[0][0] == 'X') return 1; // X wins
if (point[0][1] == point[1][1] && point[1][1] == point[2][1] && point[0][1] == 'X') return 1; // X wins
if (point[0][2] == point[1][2] && point[1][2] == point[2][2] && point[0][2] == 'X') return 1; // X wins
if (point[0][0] == point[1][1] && point[1][1] == point[2][2] && point[1][1] == 'X') return 1; // X wins
if (point[0][2] == point[1][1] && point[1][1] == point[2][0] && point[1][1] == 'X') return 1; // X wins
if (point[0][0] == point[0][1] && point[0][1] == point[0][2] && point[0][0] == '0') return 2; // 0 wins
if (point[1][0] == point[1][1] && point[1][1] == point[1][2] && point[1][0] == '0') return 2; // 0 wins
if (point[2][0] == point[2][1] && point[2][1] == point[2][2] && point[2][0] == '0') return 2; // 0 wins
if (point[0][0] == point[1][0] && point[1][0] == point[2][0] && point[0][0] == '0') return 2; // 0 wins
if (point[0][1] == point[1][1] && point[1][1] == point[2][1] && point[0][1] == '0') return 2; // 0 wins
if (point[0][2] == point[1][2] && point[1][2] == point[2][2] && point[0][2] == '0') return 2; // 0 wins
if (point[0][0] == point[1][1] && point[1][1] == point[2][2] && point[1][1] == '0') return 2; // 0 wins
if (point[0][2] == point[1][1] && point[1][1] == point[2][0] && point[1][1] == '0') return 2; // 0 wins
if (n == 9) return 3; // end up in a draw
else return 0; // unfinished
}
void game_player_change(char &player)
{
if (player == 'X')
player = '0'; // X -> 0
else player = 'X';// 0 -> X
}
int main()
{
// Print an empty board.
std::cout << "| |1|2|3|" << endl;
std::cout << "|A| | | |" << endl;
std::cout << "|B| | | |" << endl;
std::cout << "|C| | | |\n" << endl;
while (win_lose(point_now, step_count) == 0)
{
string location;
unsigned location_letter = 0; // A/B/C
unsigned location_number = 0; // 1/2/3
begin: // a label
std::cout << "Player " << player << ", make your move: ";
cin >> location; // input the location (e.g. B2)
std::cout << endl;
switch (location[0])
{
case 'a': case 'A':
location_letter = 0;
break;
case 'b': case 'B':
location_letter = 1;
break;
case 'c': case 'C':
location_letter = 2;
break;
default: // illegal input
location_letter = 100; // indicate an error
break;
}
switch (location[1])
{
case '1':
location_number = 0;
break;
case '2':
location_number = 1;
break;
case '3':
location_number = 2;
break;
default: // illegal input
location_number = 100; // indicate an error
break;
}
try
{
if (location_letter != 100 && location_number != 100 && point_now[location_letter][location_number] == ' ')
point_now[location_letter][location_number] = player;
else throw runtime_error("Illegal input!");
}
catch(runtime_error err)
{
std::cout << err.what() << "\nTry Again? Rnter Y or N." << endl;
char decision;
cin >> decision;
if (!cin || decision == 'n' || decision == 'N')
break;
else
{
cout << endl;
goto begin; // go back to the label 'begin'
}
}
// Print the board
std::cout << "| |1|2|3|" << endl;
std::cout << "|A|" << point_now[0][0] << "|" << point_now[0][1] << "|" << point_now[0][2] << "|" << endl;
std::cout << "|B|" << point_now[1][0] << "|" << point_now[1][1] << "|" << point_now[1][2] << "|" << endl;
std::cout << "|C|" << point_now[2][0] << "|" << point_now[2][1] << "|" << point_now[2][2] << "|" << endl;
game_player_change(player); // change the player
++step_count; // count one more time
std::cout << endl;
}
int final_result = win_lose(point_now, step_count);
switch (final_result)
{
case 1:
std::cout << "Congratulations! The winner is X." << endl;
break;
case 2:
std::cout << "Congratulations! The winner is 0." << endl;
break;
case 3:
std::cout << "The game ended in a draw." << endl;
break;
}
return 0;
}
一起加油!!