关于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语句不可以三连等!!!