在<jump into c++>中有一道课后题P104第三题
另外看到机核网微博里写的一个在美国学习游戏开发的人的课程设计里面也提到了这个东西
觉得有必要自己实现一下
这里做的是正方形棋盘
开发环境为vs2015
#include <iostream>
#include <ctime>
#include <iomanip>
const int maximum = 10;
int custom_size = 5;
void show(char arr[], int size) {
std::cout << " ";
for (int i = 0; i < size; ++i) {
std::cout << i << " ";
}
std::cout << std::endl;
std::cout << " ";
for (int i = 0; i < size * 2 + 1; ++i) {
std::cout << "-";
}
std::cout << std::endl;
for (int i = 0; i < size; ++i) {
std::cout << i << "| ";
for (int j = 0; j < size; ++j) {
std::cout << arr[i*size + j] << " ";
}
std::cout << "|" << std::endl;
}
std::cout << " ";
for (int i = 0; i < size * 2 + 1; ++i) {
std::cout << "-";
}
std::cout << std::endl;
}
bool isValidInput(char arr[], int x, int y) {
if (x > custom_size || y > custom_size || x < 0 || y < 0)
return false;
return arr[x + y*custom_size] == ' ';
}
bool checkWin(char arr[], int x , int y, char mark) {
bool bContinuous = true;
int left_count = 4;
int adjacent_count = 0;
// \ orientation
adjacent_count = 0;
// out direction
bContinuous = true;
for (int i = 0; i < 4 && x - i >= 0 && x - i < custom_size && y - i >= 0 && y - i < custom_size; ++i) {
if (arr[x - i + (y - i) * custom_size] == mark) {
--left_count;
if (++adjacent_count == 4) {
return true;
}
} else {
bContinuous = false;
}
}
if (bContinuous) {
// in direction, i start from 1, to avoid calculating the original point twice
for (int i = 1; i <= left_count && x + i >= 0 && x + i < custom_size && y + i >= 0 && y + i < custom_size; ++i) {
if (arr[x + i + (y + i) * custom_size] == mark) {
if (++adjacent_count == 4) {
return true;
}
}
else {
bContinuous = false;
}
}
}
if (bContinuous && adjacent_count >= 4) {
return true;
}
// | orientation
left_count = 4;
adjacent_count = 0;
// out direction
bContinuous = true;
for (int i = 0; i < 4 && x >= 0 && x < custom_size && y - i < custom_size && y - i >= 0; ++i) {
if (arr[x + (y - i) * custom_size] == mark) {
--left_count;
if (++adjacent_count == 4) {
return true;
}
}
else {
bContinuous = false;
}
}
if (bContinuous) {
// in direction, i start from 1, to avoid calculating the original point twice
for (int i = 1; i <= left_count && x >= 0 && x < custom_size && y + i >= 0 && y + i < custom_size; ++i) {
if (arr[x + (y + i) * custom_size] == mark) {
if (++adjacent_count == 4) {
return true;
}
}
else {
bContinuous = false;
}
}
}
if (bContinuous && adjacent_count >= 4) {
return true;
}
// / orientation
left_count = 4;
adjacent_count = 0;
// out direction
bContinuous = true;
for (int i = 0; i < 4 && x + i >= 0 && x + i < custom_size && y - i < custom_size && y - i >= 0; ++i) {
if (arr[x + i + (y - i) * custom_size] == mark) {
--left_count;
if (++adjacent_count == 4) {
return true;
}
}
else {
bContinuous = false;
}
}
if (bContinuous) {
// in direction, i start from 1, to avoid calculating the original point twice
for (int i = 1; i <= left_count && x - i >= 0 && x - i < custom_size && y + i >= 0 && y + i < custom_size; ++i) {
if (arr[x - i + (y + i) * custom_size] == mark) {
if (++adjacent_count == 4) {
return true;
}
}
else {
bContinuous = false;
}
}
}
if (bContinuous && adjacent_count >= 4) {
return true;
}
// - orientation
left_count = 4;
adjacent_count = 0;
// out direction
bContinuous = true;
for (int i = 0; i < 4 && x + i >= 0 && x + i < custom_size && y >= 0 && y < custom_size; i++) {
--left_count;
if (arr[x + i + y * custom_size] == mark) {
++adjacent_count;
}
else {
bContinuous = false;
}
}
if (bContinuous) {
// in direction
for (int i = 0; i <= left_count && x - i >= 0 && x - i < custom_size && y >= 0 && y < custom_size; i++) {
if (arr[x - i + y* custom_size] == mark) {
++adjacent_count;
}
else {
bContinuous = false;
}
}
}
if (bContinuous && adjacent_count >= 4) {
return true;
}
return false;
}
int main() {
bool bInputOk = false;
char arr[maximum*maximum];
while (!bInputOk) {
std::cout << "Please set the size of game(maximum 10):" << std::endl;
std::cin >> custom_size;
if (custom_size > 0 && custom_size <= maximum) {
bInputOk = true;
}
}
for (int i = 0; i < maximum*maximum; ++i) {
arr[i] = ' ';
}
show(arr, custom_size);
std::cout << "<<<<game start>>>>" << std::endl;
bool bGameover = false;
while (!bGameover) {
int p1x, p1y, p2x, p2y;
bInputOk = false;
while (!bInputOk) {
std::cout << "player1 input x and y(maxsize:" << custom_size << ")" << std::endl;
std::cin >> p1x >> p1y;
std::cin.ignore();
bInputOk = isValidInput(arr, p1x, p1y);
}
arr[p1x + p1y*custom_size] = 'X';
show(arr, custom_size);
bGameover = checkWin(arr, p1x, p1y, 'X');
if (bGameover) {
std::cout << "player1 win" << std::endl;
break;
}
bInputOk = false;
while (!bInputOk) {
std::cout << "player2 input x and y(maxsize:" << custom_size << ")" << std::endl;
std::cin >> p2x >> p2y;
std::cin.ignore();
bInputOk = isValidInput(arr, p2x, p2y);
}
arr[p2x + p2y*custom_size] = 'O';
show(arr, custom_size);
bGameover = checkWin(arr, p2x, p2y, 'O');
if (bGameover) {
std::cout << "player2 win" << std::endl;
break;
}
}
std::cin.get();
return 0;
}