Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cstdlib>
- #include <ctime>
- #include <math.h>
- const int maxSize = 10;
- const int nSym = 4;
- const int s = 2 * maxSize * (maxSize + 1);
- const char sym[nSym] = {' ' , 'X' , 'O' , '|'};
- class coord {
- public:
- int x;
- int y;
- };
- class coordMass {
- public:
- int x[s];
- int y[s];
- int val[s];
- int vsVal[s];
- int rept[s];
- };
- int size = 3;
- int map[maxSize][maxSize];
- int x , y;
- int player , type;
- coord move;
- char a;
- char b[5];
- int random (int a) {
- a = round(a * (double)(rand()%100) / 100);
- if (a == 0)
- a = 1;
- return a;
- }
- void PrintMap() {
- int i , j;
- for (i = 0 ; i < size ; i++) {
- printf("%c",sym[nSym - 1]);
- for (j = 0; j < size ; j++)
- printf("%c%c",sym[map[i][j]],sym[nSym - 1]);
- printf("\n");
- }
- }
- void ClearMap() {
- int i , j;
- for (i = 0 ; i < size ; i++)
- for (j = 0 ; j < size ; j++)
- map[i][j] = 0;
- }
- int vs(int player) {
- if (player == 1)
- return 2;
- if (player == 2)
- return 1;
- }
- coord CalcMove(int player) {
- coord PlayerMove;
- int nMove = 0 , nMoveRept = 0;
- coordMass mMove , mMoveRept;
- int i , j , n , vsN , x , y;
- int valMax = 0 , vsValMax = 0;
- int reptMax = 0 , nMax = 0 ;
- int rpos;
- srand(time(NULL));
- for (i = 0 ; i < size ; i++) {
- n = 0;
- vsN = 0;
- for (j = 0; j < size ; j++) {
- if (map[i][j] == player)
- n += 1;
- if (map[i][j] == vs(player))
- vsN += 1;
- mMove.x[size * nMove + j] = i;
- mMove.y[size * nMove + j] = j;
- }
- for (j = 0; j < size ; j++)
- if (map[i][j] == 0) {
- mMove.val[size * nMove + j] = n;
- mMove.vsVal[size * nMove + j] = vsN;
- }
- else {
- mMove.val[size * nMove + j] = -1;
- mMove.vsVal[size * nMove + j] = -1;
- }
- nMove += 1;
- }
- for (j = 0 ; j < size ; j++) {
- n = 0;
- vsN = 0;
- for (i = 0; i < size ; i++) {
- if (map[i][j] == player)
- n += 1;
- if (map[i][j] == vs(player))
- vsN += 1;
- mMove.x[size * nMove + i] = i;
- mMove.y[size * nMove + i] = j;
- }
- for (i = 0; i < size ; i++)
- if (map[i][j] == 0) {
- mMove.val[size * nMove + i] = n;
- mMove.vsVal[size * nMove + i] = vsN;
- }
- else {
- mMove.val[size * nMove + i] = -1;
- mMove.vsVal[size * nMove + i] = -1;
- }
- nMove += 1;
- }
- for (j = 0 ; j <= 1 ; j++) {
- n = 0;
- vsN = 0;
- for (i = 0 ; i < size ; i++) {
- x = i;
- y = (j * 2 - 1) * (j * (size - 1) - i);
- if (map[x][y] == player)
- n += 1;
- if (map[x][y] == vs(player))
- vsN += 1;
- mMove.x[size * nMove + i] = x;
- mMove.y[size * nMove + i] = y;
- }
- for (i = 0 ; i < size ; i++) {
- x = i;
- y = (j * 2 - 1) * (j * (size - 1) - i);
- if (map[x][y] == 0) {
- mMove.val[size * nMove + i] = n;
- mMove.vsVal[size * nMove + i] = vsN;
- }
- else {
- mMove.val[size * nMove + i] = -1;
- mMove.vsVal[size * nMove + i] = -1;
- }
- }
- nMove += 1;
- }
- nMove *= size;
- for (i = 0 ; i < s ; i++)
- mMoveRept.rept[i] = 0;
- for (i = 0 ; i < nMove ; i++)
- if (mMove.val[i] != 0 and mMove.vsVal[i] != 0)
- mMove.val[i] = -1;
- for (i = 0 ; i < nMove ; i++) {
- if (mMove.val[i] != -1)
- for (j = 0 ; j <= nMoveRept ; j++) {
- if (mMove.x[i] != -1 and mMove.x[i] == mMoveRept.x[j] and mMove.y[i] == mMoveRept.y[j] and mMove.val[i] == mMoveRept.val[j] and mMove.vsVal[i] == mMoveRept.vsVal[j]) {
- mMoveRept.rept[j] += 1;
- mMove.x[i] = -1;
- break;
- }
- }
- if (mMove.val[i] != -1 and mMove.x[i] != -1) {
- mMoveRept.x[nMoveRept] = mMove.x[i];
- mMoveRept.y[nMoveRept] = mMove.y[i];
- mMoveRept.val[nMoveRept] = mMove.val[i];
- mMoveRept.vsVal[nMoveRept] = mMove.vsVal[i];
- mMoveRept.rept[nMoveRept] += 1;
- nMoveRept += 1;
- mMove.x[i] = -1;
- }
- }
- for (i = 0; i < nMoveRept ; i++) {
- if (mMoveRept.val[i] > valMax)
- valMax = mMoveRept.val[i];
- if (mMoveRept.vsVal[i] > vsValMax)
- vsValMax = mMoveRept.vsVal[i];
- }
- if (valMax >= vsValMax) {
- for (i = 0 ; i < nMoveRept ; i++)
- if (mMoveRept.val[i] == valMax and mMoveRept.rept[i] > reptMax)
- reptMax = mMoveRept.rept[i];
- vsValMax = 0;
- }
- else {
- for (i = 0 ; i < nMoveRept ; i++)
- if (mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] > reptMax)
- reptMax = mMoveRept.rept[i];
- valMax = 0;
- }
- for (i = 0 ; i < nMoveRept ; i++)
- if (mMoveRept.val[i] == valMax and mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] == reptMax)
- nMax += 1;
- rpos = random(nMax);
- i = 0;
- while (rpos > 0) {
- if (mMoveRept.val[i] == valMax and mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] == reptMax)
- rpos -= 1;
- i += 1;
- }
- rpos = i - 1;
- if (nMoveRept > 0) {
- PlayerMove.x = mMoveRept.x[rpos];
- PlayerMove.y = mMoveRept.y[rpos];
- }
- else {
- n = 0;
- for (i = 0 ; i < size ; i++)
- for (j = 0 ; j < size ; j++)
- if (map[i][j] == 0)
- n += 1;
- rpos = random(n);
- for (i = 0 ; i < size ; i++)
- for (j = 0 ; j < size ; j++)
- if (map[i][j] == 0) {
- rpos -= 1;
- if (rpos == 0) {
- PlayerMove.x = i;
- PlayerMove.y = j;
- break;
- }
- }
- }
- return PlayerMove;
- }
- int CheckGame() {
- int i , j , nPlayer , nVsPlayer , stat = 0 , x , y , player = 1;
- for (i = 0 ; i < size ; i++) {
- nPlayer = 0;
- nVsPlayer = 0;
- for (j = 0 ; j < size ; j++)
- if (map[i][j] == player)
- nPlayer += 1;
- else
- if (map[i][j] == vs(player))
- nVsPlayer += 1;
- if (nPlayer == size){
- stat = player;
- break;
- }
- if (nVsPlayer == size) {
- stat = vs(player);
- break;
- }
- }
- if (stat == 0)
- for (j = 0 ; j < size ; j++) {
- nPlayer = 0;
- nVsPlayer = 0;
- for (i = 0 ; i < size ; i++)
- if (map[i][j] == player)
- nPlayer += 1;
- else
- if (map[i][j] == vs(player))
- nVsPlayer += 1;
- if (nPlayer == size){
- stat = player;
- break;
- }
- if (nVsPlayer == size) {
- stat = vs(player);
- break;
- }
- }
- if (stat == 0)
- for (j = 0 ; j <= 1 ; j++) {
- nPlayer = 0;
- nVsPlayer = 0;
- for (i = 0 ; i < size ; i++) {
- x = i;
- y = (j * 2 - 1) * (j * (size - 1) - i);
- if (map[x][y] == player)
- nPlayer += 1;
- if (map[x][y] == vs(player))
- nVsPlayer += 1;
- }
- if (nPlayer == size) {
- stat = player;
- break;
- }
- if (nVsPlayer == size) {
- stat = vs(player);
- break;
- }
- }
- if (stat == 0) {
- x = 0;
- for (i = 0 ; i < size ; i++)
- for (j = 0 ; j < size ; j++)
- if (map[i][j] != 0)
- x += 1;
- if (x == size * size)
- stat = 3;
- }
- return stat;
- }
- int main() {
- while (1) {
- system("cls");
- printf("Select a size of game (min 3 , max 10) :\n");
- do {
- scanf("%i",&size);
- if (not (size >= 3 and size <= 10))
- printf("Eror!\n");
- } while (not (size >= 3 and size <= 10));
- system("cls");
- printf("Select a type of game :\n");
- printf("1 - game with computer\n");
- printf("2 - game with friend\n");
- do {
- scanf("%i",&type);
- if (not (type >= 1 and type <= 2))
- printf("Eror!\n");
- } while (not (type >= 1 and type <= 2));
- system("cls");
- if (type == 1) {
- printf("Select a cross or toe :\n");
- printf("1 - cross\n");
- printf("2 - toe\n");
- do {
- scanf("%i",&player);
- if (not (player >= 1 and player <= 2))
- printf("Eror!\n");
- } while (not (player >= 1 and player <= 2));
- system("cls");
- }
- ClearMap();
- while (CheckGame() == 0) {
- if (type == 2) {
- PrintMap();
- printf("Cross move:\n");
- do {
- scanf("%i %i",&x,&y);
- if (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0))
- printf("Eror!\n");
- } while (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0));
- map[x-1][y-1] = 1;
- if (CheckGame() != 0) {
- break;
- }
- else
- system("cls");
- PrintMap();
- printf("Toe move:\n");
- do {
- scanf("%i %i",&x,&y);
- if (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0))
- printf("Eror!\n");
- } while (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0));
- map[x-1][y-1] = 2;
- if (CheckGame() != 0) {
- break;
- }
- else
- system("cls");
- }
- if (type == 1 and player == 2) {
- move = CalcMove(vs(player));
- map[move.x][move.y] = vs(player);
- if (CheckGame() != 0) {
- break;
- }
- }
- if (type == 1) {
- PrintMap();
- printf("Your move :\n");
- do {
- scanf("%i %i",&x,&y);
- if (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0))
- printf("Eror!\n");
- } while (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0));
- map[x-1][y-1] = player;
- if (CheckGame() != 0) {
- break;
- }
- else
- system("cls");
- }
- if (type == 1 and player == 1) {
- move = CalcMove(vs(player));
- map[move.x][move.y] = vs(player);
- if (CheckGame() != 0) {
- break;
- }
- }
- }
- system("cls");
- PrintMap();
- if (type == 1 and CheckGame() == player)
- printf("You win!\n");
- else
- if (type == 1 and CheckGame() == vs(player))
- printf("You lose!\n");
- if (type == 2) {
- if (CheckGame() == 1)
- printf("Cross win!\n");
- if (CheckGame() == 2)
- printf("Toe win!\n");
- }
- if (CheckGame() == 3)
- printf("Draw!\n");
- printf("Do you want to try again ?\n");
- printf("Print Yes to continue\n");
- scanf("%s",&b);
- if (not (b[0] == 'Y' and b[1] == 'e' and b[2] == 's'))
- break;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment