一个适合新手, 简单易懂的进子棋游戏代码
#include <iostream>#include <stdlib.h>
using namespace std;
class CGame
{
public:
CGame();
void Print();
int Cal(int i, int j);
bool Judge(int i, int j, int k);
void Play();
private:
char status[9];
int score[9];
int fail, counter;
};
CGame::CGame()
{
for (int i = 0; i < 9; i++)
{
status[i] = '1' + i;
};
counter = 0;
fail = 0;
};
void CGame::Print()
{
system("cls");
cout << " " << status[0] << " | " << status[1] << " | " << status[2] << endl;
cout << "--+--+--+--+--+--" << endl;
cout << " " << status[3] << " | " << status[4] << " | " << status[5] << endl;
cout << "--+--+--+--+--+--" << endl;
cout << " " << status[6] << " | " << status[7] << " | " << status[8] << endl;
};
int CGame::Cal(int i, int j)
{
int result = 0;
if ((status[i - 1] == 'X') && (status[j - 1] == 'X'))
{
result = 50;
fail = 2;
}
else if ((status[i - 1] == 'O') && (status[j - 1] == 'O'))
{
result = 25;
}
else if (((status[i - 1] == 'X') && (status[j - 1] < 'A')) || ((status[i - 1] < 'A') && (status[j - 1] == 'X')))
{
result = 10;
}
else if (((status[i - 1] == 'O') && (status[j - 1] < 'A')) || ((status[i - 1] < 'A') && (status[j - 1] == 'O')))
{
result = 4;
};
return result;
};
bool CGame::Judge(int i, int j, int k)
{
return ((status[i - 1] == 'O') && (status[j - 1] == 'O') && (status[k - 1] == 'O'));
};
void CGame::Play()
{
int n = 0, max = 0, temp = 0;
char ch;
Print();
cout <<"Choose: Start First(input F) or Second(input S)" << endl;
cin >> ch;
if ((ch == 's') && (ch == 'S'))
{
status[4] = 'X';
counter++;
Print();
cout << "Computer Put at NO.5." << endl;
};
do
{
do
{
cout << "Please Put on (1 - 9)." << endl;
cin >> n;
}while (status[n - 1] >= 'A');
status[n - 1] = 'O';
counter++;
Print();
if (Judge(1, 2, 3) || Judge(4, 5, 6) || Judge(7, 8, 9) || Judge(1, 4, 7) || Judge(2, 5, 8) || Judge(3, 6, 9) || Judge(1, 5, 9) || Judge(3, 5, 7))
{
fail = 1;
};
if ((fail < 1) && (counter < 9))
{
for (int k = 0; k < 9; k++)
{
if (status[k] < 'A')
{
switch (k)
{
case 0: score[0] = Cal(2, 3) + Cal(4, 7) + Cal(5, 9);
break;
case 1: score[1] = Cal(1, 3) + Cal(5, 8);
break;
case 2: score[2] = Cal(1, 2) + Cal(6, 9) + Cal(5, 7);
break;
case 3: score[3] = Cal(5, 6) + Cal(1, 7);
break;
case 4: score[4] = Cal(4, 6) + Cal(2, 8) + Cal(1, 9) + Cal(3, 7);
break;
case 5: score[5] = Cal(4, 5) + Cal(3, 9);
break;
case 6: score[6] = Cal(8, 9) + Cal(1, 4) + Cal(3, 5);
break;
case 7: score[7] = Cal(7, 9) + Cal(2, 5);
break;
case 8: score[8] = Cal(7, 8) + Cal(3, 6) + Cal(1, 5);
break;
};
}
else
{
score[k] = -1;
};
};
};
max = score[0];
for (int k = 1; k < 9; k++)
{
if (score[k] > max)
{
max = score[k];
temp = k;
};
};
status[temp] = 'X';
counter++;
cout << "Computer Plays " << temp + 1 << "." << endl;
Print();
}while ((fail <= 0) && (counter < 9));
if (fail == 0)
{
cout << "Your Draw." << endl;
}
else if (fail == 1)
{
cout << "You Won." << endl;
}
else
{
cout << "You Lose." << endl;
};
};
|
|
|
|
|
#include "CGame.h"
int main()
{
int a;
CGame game;
game.Play();
return 0;
cout << "Press Any Key To End And Enter." << endl;
cin >> a;
};