【C++】TicTacToe(井字棋)小游戏

关于TicTacToe介绍

话不多说上源码!

头文件👇

#pragma once
#include<iostream>
using namespace std;
class TicTacToe {
private:
	char currentPlayer;
	char** board;
public:
	TicTacToe();//构造函数
	void print();//打印棋盘状态
	char getCurrentPlayer();//返回当前玩家
	bool isDone();//判断是否分出胜负或无路可走
	char getWinner();//返回赢家信息,如果没有赢家返回'-'
	bool isValidMove(int rowValue, int columnValue);//检查输入是否合法
	bool makeMove(int rowValue, int columnValue);//将指定位置设置为玩家符号,并切换玩家
};
TicTacToe::TicTacToe() {
	currentPlayer = 'X';
	board = new char* [3];
	for (int i = 0; i < 3; i++) {
		board[i] = new char[3];
		for (int j = 0; j < 3; j++) {
			board[i][j] = '-';
		}
	}
}
void TicTacToe::print() {
	cout << endl;
	cout << " ";
	for (int i = 1; i <= 3; i++) {
		cout.width(4);
		cout << i;
	}
	cout << endl;
	for (int i = 0; i < 3; i++) {
		cout << i + 1;
		for (int j = 0; j < 3; j++) {
			cout.width(4);
			cout << board[i][j];
		}
		cout << endl;
	}
	cout << endl;
}
char TicTacToe::getCurrentPlayer() {
	return currentPlayer;
}
bool TicTacToe::isDone() {
	if (board[0][0] != '-' && board[0][0] == board[0][1] && board[0][1] == board[0][2]) {
		return true;
	}
	if (board[1][0] != '-' && board[1][0] == board[1][1] && board[1][1] == board[1][2]) {
		return true;
	}
	if (board[2][0] != '-' && board[2][0] == board[2][1] && board[2][1] == board[2][2]) {
		return true;
	}
	if (board[0][0] != '-' && board[0][0] == board[1][0] && board[1][0] == board[2][0]) {
		return true;
	}
	if (board[0][1] != '-' && board[0][1] == board[1][1] && board[1][1] == board[2][1]) {
		return true;
	}
	if (board[0][2] != '-' && board[0][2] == board[1][2] && board[1][2] == board[2][2]) {
		return true;
	}
	if (board[0][0] != '-' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
		return true;
	}
	if (board[0][2] != '-' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
		return true;
	}
	for (int i = 0; i < 3; i++) {
		if (board[i][0] == '-' || board[i][1] == '-' || board[i][2] == '-') {
			return false;
		}
	}
	return true;
}
char TicTacToe::getWinner() {
	if (isDone()) {
	return currentPlayer;
	}
	return '-';
}
bool TicTacToe::isValidMove(int rowValue, int columnValue) {
	if (rowValue > 0 && rowValue <= 3 && columnValue > 0 && columnValue <= 3) {
		if (board[rowValue - 1][columnValue - 1] == '-') {
			return true;
		}
	}
	return false;
}
bool TicTacToe::makeMove(int rowValue, int columnValue) {
	board[rowValue - 1][columnValue - 1] = currentPlayer;
	if (currentPlayer == 'X') {
		currentPlayer = 'O';
	}
	else {
		currentPlayer = 'X';
	}
	return true;
}

源文件👇

#include"TicTacToe.h"

int main(){
	cout << "Welcome to TicTacToe, do you want to start a new game ? (Y/N)" << endl;
	char isGo;
	while (cin >> isGo) {
		if (isGo == 'Y' || isGo == 'y') {
			TicTacToe test;
			int row = 0, column = 0;
			while (!test.isDone()) {
				test.print();
				do{
					cout << "Now it's turn for " << test.getCurrentPlayer() << " move, please input the row:";
					cin >> row;
					cout << "Now it's turn for " << test.getCurrentPlayer() << " move, please input the column:";
					cin >> column;
				} while (!test.isValidMove(row, column));
				test.makeMove(row, column);
			}
			cout << test.getWinner() << " is the winner, congratulations!" << endl;
			cout << "Do you want to start a new game ? (Y/N)" << endl;
		}
		else if(isGo == 'N' || isGo == 'n') {
			cout << "Goodbye, welcome play again!";
			break;
		}
	}
}

经验总结

①C++源文件包含头文件不带书名号

②头文件中包含的头文件以及std都会包含进源文件

③C++的if语句不可以三连等!!!

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mitch311

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值