时间有些急,主代码测试了差不多,细节没有处理,也没这么麻烦,只是我想多涉及一些知识
mian.cpp
#include <iostream>
#include "game.h"
#include <string>
int main(int argc, char const *argv[])
{
int num1, num2;
Game game;
Player player1("XXX");
Player player2("OOO");
game.game_init();
while (1)
{
std::cout << player1.name << "[ " << player1.cp
<< " ] : ";
std::cin >> num1 >> num2;
game.chessboard_chang(player1, num1, num2);
std::cout << player2.name << "[ " << player2.cp
<< " ] : ";
std::cin >> num1 >> num2;
game.chessboard_chang(player2, num1, num2);
}
return 0;
}
game.h
#if !defined(__GAME__H)
#define __GAME__H
#define FAIL 0
#define OK 1
class Player;
class Game
{
protected:
friend void end_handler(Player &player, Game &game);
public:
Game(/* args */);
~Game();
public:
int game_init();
int chessboard_chang(Player &player, int x, int y);
void display();
int game_destory();
};
class Player : public Game
{
private:
int count = 0; // 观察连赢次数
public:
char name[12];
char cp;
public:
Player();
Player(const char *);
~Player();
public:
// int chessboard_chang(int &x, int &y);
void display(); // 虚函数
};
#endif // __GAME__H
game.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include "game.h"
#define PLAY_MAX_NUM 2
#define XMAX 3
#define YMAX 3
#define DATAFILE "./data.txt"
// typedef struct Player_st
// {
// char player_name[12];
// int play_num;
// char cp;
// } Player_st;
// static Player_st players[2];
static int player_count = 0;
static char ch_arrs[YMAX][XMAX];
static int bout_max_num = 9;
Game game;
Game::Game()
{
}
Game::~Game()
{
}
static void ch_zero()
{
for (int y = 0; y < YMAX; y++)
{
for (int x = 0; x < XMAX; x++)
{
ch_arrs[y][x] = 0;
}
}
}
static void ch_char(int x, int y, char cp)
{
for (int i = 0; i <= YMAX; i++)
{
if (i == y)
{
for (int j = 0; j <= XMAX; j++)
{
if (j == x)
{
ch_arrs[i][j] = cp;
break;
}
}
}
}
}
/**
* 初始化棋盘:创建一个data.txt文件
* 0 1 2
* 0 - - -
* 1 - - -
* 2 - - -
*/
int Game::game_init()
{
std::ofstream out_file(DATAFILE, std::ios::out);
if (!out_file)
return FAIL;
std::string chessboard_style = "\t0\t1\t2\n0\t-\t-\t-\n1\t-\t-\t-\n2\t-\t-\t-\n";
std::cout << chessboard_style;
out_file << chessboard_style;
out_file.close();
ch_zero();
std::cout << "游戏开始" << std::endl;
std::cout << std::endl;
return OK;
}
/**
* 销毁这个游戏
*/
int Game::game_destory()
{
if (!remove(DATAFILE))
return FAIL;
return OK;
}
// 判断坐标是否超出范围
static bool is_correct(int vaule)
{
if (vaule > 2)
return false;
return true;
}
static size_t find_x(std::string data, int x)
{
size_t index = 0;
if (x == 0)
{
index = data.find('-', index);
return index;
}
for (int i = 0; i <= x; i++)
{
index = data.find('-', index);
if (index == std::string::npos)
return -1;
if (x > 0)
index++;
}
return --index;
}
static int win_handler(char cp)
{
// win有8种情况
int win_x = 0;
int win_y = 0;
char char_x[3];
char char_y[3];
int flag = 0;
// 交叉
if (ch_arrs[0][0] == cp && ch_arrs[1][1] == cp && ch_arrs[2][2] == cp)
return 1;
if (ch_arrs[0][2] == cp && ch_arrs[1][1] == cp && ch_arrs[2][0] == cp)
return 1;
// x
if (ch_arrs[0][0] == cp && ch_arrs[0][1] == cp && ch_arrs[0][2] == cp)
return 1;
if (ch_arrs[1][2] == cp && ch_arrs[1][1] == cp && ch_arrs[1][0] == cp)
return 1;
if (ch_arrs[2][2] == cp && ch_arrs[2][1] == cp && ch_arrs[2][0] == cp)
return 1;
// y
if (ch_arrs[0][1] == cp && ch_arrs[1][1] == cp && ch_arrs[2][1] == cp)
return 1;
if (ch_arrs[0][0] == cp && ch_arrs[1][0] == cp && ch_arrs[2][0] == cp)
return 1;
if (ch_arrs[0][2] == cp && ch_arrs[1][2] == cp && ch_arrs[2][2] == cp)
return 1;
return -1;
}
void end_handler(Player &player, Game &game)
{
if (bout_max_num == 0)
{
std::cout << "平局" << std::endl <<std::endl;
bout_max_num = 0;
game.game_init();
}
if (win_handler(player.cp) == 1)
{
std::cout << player.name << "获胜" << std::endl;
}
}
int Game::chessboard_chang(Player &player, int x, int y)
{
if (!is_correct(x))
return FAIL;
if (!is_correct(y))
return FAIL;
std::ifstream input(DATAFILE);
if (!input)
{
std::cout << "input is fail" << std::endl;
exit(1);
}
std::string line, result;
int i = 0;
while (std::getline(input, line))
{
if (i == y + 1)
{
size_t index = find_x(line, x);
if (index < 0)
std::cout << "find_x is fail" << std::endl;
line.at(index) = player.cp;
ch_char(x, y, player.cp);
}
result = result + line + '\n';
i++;
}
input.close();
std::cout << result << std::endl;
std::ofstream output_(DATAFILE, std::ios::out);
if (!output_)
{
std::cout << "output is fail" << std::endl;
exit(1);
}
output_ << result;
output_.close();
bout_max_num--;
end_handler(player, game);
return 0;
}
void Game::display()
{
std::cout << "当前有" << player_count << "位玩家" << std::endl;
}
Player::Player(/* args */)
{
}
Player::Player(const char *name)
{
strcpy(this->name, name);
if (player_count == 0)
this->cp = 'X';
else
this->cp = 'O';
player_count++;
}
Player::~Player()
{
}
运行结果