Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- char square[9], squareSave[9];
- char player, ai;
- int winner = 0, winnerSave = 0, currentStep, aiPick = -1, chosen[9];
- void clrscr() {
- system("@cls||clear");
- }
- void outPut() {
- int line = 0;
- printf("\n------------------------------------------------\n");
- for (int i = 0; i != 9; i++) {
- if (i / 3 != line) {
- printf("\n\n");
- line++;
- }
- printf("| %c |", square[i]);
- if ((i + 1) % 3 == 0) {
- printf(" |(%c)|(%c)|(%c)|", line * 3 + 1 + '0', line * 3 + 2 + '0', line * 3 + 3 + '0');
- }
- }
- printf("\n------------------------------------------------\n");
- }
- void outPutSave() {
- int line = 0;
- printf("\n------------------------------------------------\n");
- for (int i = 0; i != 9; i++) {
- if (i / 3 != line) {
- printf("\n\n");
- line++;
- }
- printf("| %c |", squareSave[i]);
- }
- printf("\n------------------------------------------------\n");
- }
- void set0() {
- for (int i = 0; i != 9; i++) {
- square[i] = '.';
- chosen[i] = 9;
- }
- }
- void set0Save() {
- for (int i = 0; i != 9; i++) {
- squareSave[i] = '.';
- }
- }
- void save() {
- for (int i = 0; i != 9; i++) {
- squareSave[i] = square[i];
- }
- }
- void load() {
- for (int i = 0; i != 9; i++) {
- square[i] = squareSave[i];
- }
- }
- void playerStep() {
- int playerPick = -1, checker = 0;
- char playerPickChar[1];
- printf("\nPick a free square(1-9) to place %c: ", player);
- do {
- int res;
- do {
- res = scanf("%d", &playerPick);
- while (getchar() != '\n');
- if (res != 1) printf("\nError. Pick a free square(1-9) to place %c: ", player);
- } while (res != 1);
- if (playerPick < 1 || playerPick > 9 || square[playerPick - 1] != '.') {
- printf("\nError. Pick a free square(1-9) to place %c: ", player);
- }
- else checker = 1;
- } while (checker == 0);
- square[playerPick - 1] = player;
- chosen[currentStep - 1] = playerPick - 1;
- }
- void playerChoise() {
- int playerD = -1, playerDReserv;
- char playerDChar;
- clrscr();
- printf("\nChose your fighter(X = 1 | O = 0): ");
- int checker = 0;
- do {
- int res;
- do {
- res = scanf("%d", &playerD);
- while (getchar() != '\n');
- if (res != 1) {
- clrscr();
- printf("\nChose your fighter(X = 1 | O = 0): ");
- }
- } while (res != 1);
- if (playerD == 0 || playerD == 1) {
- checker = 1;
- break;
- }
- else {
- clrscr();
- printf("\nChose your fighter(X = 1 | O = 0): ");
- }
- } while (checker == 0);
- if (playerD == 1) {
- player = 'X';
- ai = 'O';
- }
- else {
- player = 'O';
- ai = 'X';
- }
- return;
- }
- void winCheck() {
- winner = 0;
- if (square[0] == square[1] && square[1] == square[2]) {
- if (square[0] == 'X') winner = 1;
- }
- if (square[3] == square[4] && square[4] == square[5]) {
- if (square[3] == 'X') winner = 1;
- }
- if (square[6] == square[7] && square[7] == square[8]) {
- if (square[6] == 'X') winner = 1;
- }
- if (square[0] == square[3] && square[3] == square[6]) {
- if (square[0] == 'X') winner = 1;
- }
- if (square[1] == square[4] && square[4] == square[7]) {
- if (square[1] == 'X') winner = 1;
- }
- if (square[2] == square[5] && square[5] == square[8]) {
- if (square[2] == 'X') winner = 1;
- }
- if (square[0] == square[4] && square[4] == square[8]) {
- if (square[0] == 'X') winner = 1;
- }
- if (square[2] == square[4] && square[4] == square[6]) {
- if (square[2] == 'X') winner = 1;
- }
- if (square[0] == square[1] && square[1] == square[2]) {
- if (square[0] == 'O') winner = 2;
- }
- if (square[3] == square[4] && square[4] == square[5]) {
- if (square[3] == 'O') winner = 2;
- }
- if (square[6] == square[7] && square[7] == square[8]) {
- if (square[6] == 'O') winner = 2;
- }
- if (square[0] == square[3] && square[3] == square[6]) {
- if (square[0] == 'O') winner = 2;
- }
- if (square[1] == square[4] && square[4] == square[7]) {
- if (square[1] == 'O') winner = 2;
- }
- if (square[2] == square[5] && square[5] == square[8]) {
- if (square[2] == 'O') winner = 2;
- }
- if (square[0] == square[4] && square[4] == square[8]) {
- if (square[0] == 'O') winner = 2;
- }
- if (square[2] == square[4] && square[4] == square[6]) {
- if (square[2] == 'O') winner = 2;
- }
- }
- void winCheckSave() {
- winnerSave = 0;
- if (squareSave[0] == squareSave[1] && squareSave[1] == squareSave[2]) {
- if (squareSave[0] == 'X') winnerSave = 1;
- }
- if (squareSave[3] == squareSave[4] && squareSave[4] == squareSave[5]) {
- if (squareSave[3] == 'X') winnerSave = 1;
- }
- if (squareSave[6] == squareSave[7] && squareSave[7] == squareSave[8]) {
- if (squareSave[6] == 'X') winnerSave = 1;
- }
- if (squareSave[0] == squareSave[3] && squareSave[3] == squareSave[6]) {
- if (squareSave[0] == 'X') winnerSave = 1;
- }
- if (squareSave[1] == squareSave[4] && squareSave[4] == squareSave[7]) {
- if (squareSave[1] == 'X') winnerSave = 1;
- }
- if (squareSave[2] == squareSave[5] && squareSave[5] == squareSave[8]) {
- if (squareSave[2] == 'X') winnerSave = 1;
- }
- if (squareSave[0] == squareSave[4] && squareSave[4] == squareSave[8]) {
- if (squareSave[0] == 'X') winnerSave = 1;
- }
- if (squareSave[2] == squareSave[4] && squareSave[4] == squareSave[6]) {
- if (squareSave[2] == 'X') winnerSave = 1;
- }
- if (squareSave[0] == squareSave[1] && squareSave[1] == squareSave[2]) {
- if (squareSave[0] == 'O') winnerSave = 2;
- }
- if (squareSave[3] == squareSave[4] && squareSave[4] == squareSave[5]) {
- if (squareSave[3] == 'O') winnerSave = 2;
- }
- if (squareSave[6] == squareSave[7] && squareSave[7] == squareSave[8]) {
- if (squareSave[6] == 'O') winnerSave = 2;
- }
- if (squareSave[0] == squareSave[3] && squareSave[3] == squareSave[6]) {
- if (squareSave[0] == 'O') winnerSave = 2;
- }
- if (squareSave[1] == squareSave[4] && squareSave[4] == squareSave[7]) {
- if (squareSave[1] == 'O') winnerSave = 2;
- }
- if (squareSave[2] == squareSave[5] && squareSave[5] == squareSave[8]) {
- if (squareSave[2] == 'O') winnerSave = 2;
- }
- if (squareSave[0] == squareSave[4] && squareSave[4] == squareSave[8]) {
- if (squareSave[0] == 'O') winnerSave = 2;
- }
- if (squareSave[2] == squareSave[4] && squareSave[4] == squareSave[6]) {
- if (squareSave[2] == 'O') winnerSave = 2;
- }
- }
- void aiStepX() {
- save();
- int squareScore[9], scoreBuffer, circleChecker, squareScoreMustWin[9], squareScoreMustLose[9];
- for (int i = 0; i != 9; i++) {
- squareScore[i] = 0;
- squareScoreMustWin[i] = 0;
- squareScoreMustLose[i] = 0;
- }
- for (int q = chosen[0] % 9; q != chosen[0] + 1; q++) {
- for (int w = chosen[1] % 9; w != chosen[1] + 1; w++) {
- for (int e = chosen[2] % 9; e != chosen[2] + 1; e++) {
- for (int r = chosen[3] % 9; r != chosen[3] + 1; r++) {
- for (int t = chosen[4] % 9; t != chosen[4] + 1; t++) {
- for (int y = chosen[5] % 9; y != chosen[5] + 1; y++) {
- for (int u = chosen[6] % 9; u != chosen[6] + 1; u++) {
- for (int i = chosen[7] % 9; i != chosen[7] + 1; i++) {
- for (int o = 0; o != 9; o++) {
- if (q != 10 && w != 10 && e != 10 && r != 10 && t != 10 && y != 10 && u != 10 && i != 10 && o != 10 && q != 9 && w != 9 && e != 9 && r != 9 && t != 9 && y != 9 && u != 9 && i != 9 && o != 9 &&
- w != q && e != q && r != q && t != q && y != q && u != q && i != q && o != q &&
- e != w && r != w && t != w && y != w && u != w && i != w && o != w &&
- r != e && t != e && y != e && u != e && i != e && o != e &&
- t != r && y != r && u != r && i != r && o != r &&
- y != t && u != t && i != t && o != t &&
- u != y && i != y && o != y &&
- i != u && o != u &&
- o != i) {
- set0Save();
- scoreBuffer = 0;
- circleChecker = 0;
- winnerSave = 0;
- if (squareSave[q] == '.') squareSave[q] = 'X'; else break;
- if (squareSave[w] == '.') squareSave[w] = 'O'; else break;
- if (squareSave[e] == '.') squareSave[e] = 'X'; else break;
- if (squareSave[r] == '.') squareSave[r] = 'O'; else break;
- if (squareSave[t] == '.') squareSave[t] = 'X'; else break;
- winCheckSave();
- if (winnerSave != 0) {
- circleChecker = 1;
- switch (winnerSave) {
- case 0: break;
- case 1: if (currentStep == 5) squareScoreMustWin[t]++; scoreBuffer++; break;
- default: break;
- }
- }
- if (squareSave[y] == '.') squareSave[y] = 'O'; else break;
- winCheckSave();
- if (winnerSave != 0 && circleChecker == 0) {
- circleChecker = 1;
- switch (winnerSave) {
- case 0: break;
- case 2: if (currentStep == 5) squareScoreMustLose[y]++; scoreBuffer--; break;
- default: break;
- }
- }
- if (squareSave[u] == '.') squareSave[u] = 'X'; else break;
- winCheckSave();
- if (winnerSave != 0 && circleChecker == 0) {
- switch (winnerSave) {
- case 0: break;
- case 1: if (currentStep == 7) squareScoreMustWin[u]++; scoreBuffer++; break;
- default: break;
- }
- }
- if (squareSave[i] == '.') squareSave[i] = 'O'; else break;
- winCheckSave();
- if (winnerSave != 0 && circleChecker == 0) {
- switch (winnerSave) {
- case 0: break;
- case 2: if (currentStep == 7) squareScoreMustLose[i]++; scoreBuffer--; break;
- default: break;
- }
- }
- if (squareSave[o] == '.') squareSave[o] = 'X'; else break;
- winCheckSave();
- if (winnerSave != 0 && circleChecker == 0) {
- circleChecker = 1;
- switch (winnerSave) {
- case 0: break;
- case 1: scoreBuffer++; break;
- default: break;
- }
- }
- switch (currentStep) {
- case 3: squareScore[e] = scoreBuffer + squareScore[e]; break;
- case 5: squareScore[t] = scoreBuffer + squareScore[t]; break;
- case 7: squareScore[u] = scoreBuffer + squareScore[u]; break;
- case 9: squareScore[o] = scoreBuffer + squareScore[o]; break;
- default: break;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- int minScore = 999999;
- for (int i = 0; i != 9; i++) {
- if (square[i] == '.') {
- printf("\nScore for square[%d] score: %d | mustLose: %d | mustWin: %d", i + 1, squareScore[i], squareScoreMustLose[i], squareScoreMustWin[i]);
- }
- if (i == 0) {
- minScore = squareScore[i];
- }
- else {
- if (squareScore[i] < minScore) {
- minScore = squareScore[i];
- }
- }
- }
- for (int i = 0; i != 9; i++) {
- if (squareScore[i] > minScore) {
- if (square[i] == '.') {
- minScore = squareScore[i];
- aiPick = i;
- }
- }
- int randomCount = rand() % 2;
- if (squareScore[i] == minScore && randomCount == 1 && square[i] == '.') {
- if (square[i] == '.') {
- minScore = squareScore[i];
- aiPick = i;
- }
- }
- }
- int checkerMust = 0;
- for (int i = 0; i != 9; i++) {
- if (squareScoreMustWin[i] > 0 && square[i] == '.') {
- aiPick = i;
- checkerMust = 1;
- }
- }
- int countFree = 0;
- for (int i = 0; i != 9; i++) {
- if (square[i] == '.') {
- countFree++;
- }
- }
- if (countFree == 1) {
- for (int i = 0; i != 9; i++) {
- if (square[i] == '.') {
- aiPick = i;
- }
- }
- }
- printf("\nBot picked %d\n", aiPick + 1);
- square[aiPick] = ai;
- chosen[currentStep - 1] = aiPick;
- }
- void aiStepO() {
- srand(time(NULL));
- save();
- int squareScore[9], scoreBuffer, circleChecker, squareScoreMustWin[9], squareScoreMustLose[9];
- for (int i = 0; i != 9; i++) {
- squareScore[i] = 0;
- squareScoreMustWin[i] = 0;
- squareScoreMustLose[i] = 0;
- }
- for (int q = chosen[0] % 9; q != chosen[0] + 1; q++) {
- for (int w = chosen[1] % 9; w != chosen[1] + 1; w++) {
- for (int e = chosen[2] % 9; e != chosen[2] + 1; e++) {
- for (int r = chosen[3] % 9; r != chosen[3] + 1; r++) {
- for (int t = chosen[4] % 9; t != chosen[4] + 1; t++) {
- for (int y = chosen[5] % 9; y != chosen[5] + 1; y++) {
- for (int u = chosen[6] % 9; u != chosen[6] + 1; u++) {
- for (int i = chosen[7] % 9; i != chosen[7] + 1; i++) {
- for (int o = 0; o != 9; o++) {
- if (q != 10 && w != 10 && e != 10 && r != 10 && t != 10 && y != 10 && u != 10 && i != 10 && o != 10 && q != 9 && w != 9 && e != 9 && r != 9 && t != 9 && y != 9 && u != 9 && i != 9 && o != 9 &&
- w != q && e != q && r != q && t != q && y != q && u != q && i != q && o != q &&
- e != w && r != w && t != w && y != w && u != w && i != w && o != w &&
- r != e && t != e && y != e && u != e && i != e && o != e &&
- t != r && y != r && u != r && i != r && o != r &&
- y != t && u != t && i != t && o != t &&
- u != y && i != y && o != y &&
- i != u && o != u &&
- o != i) {
- set0Save();
- scoreBuffer = 0;
- circleChecker = 0;
- winnerSave = 0;
- if (squareSave[q] == '.') squareSave[q] = 'X'; else break;
- if (squareSave[w] == '.') squareSave[w] = 'O'; else break;
- if (squareSave[e] == '.') squareSave[e] = 'X'; else break;
- if (squareSave[r] == '.') squareSave[r] = 'O'; else break;
- if (squareSave[t] == '.') squareSave[t] = 'X'; else break;
- winCheckSave();
- if (winnerSave != 0) {
- circleChecker = 1;
- switch (winnerSave) {
- case 0: break;
- case 1: if (currentStep == 4) squareScoreMustLose[t]++; scoreBuffer--; break;
- default: break;
- }
- }
- if (squareSave[y] == '.') squareSave[y] = 'O'; else break;
- winCheckSave();
- if (winnerSave != 0 && circleChecker == 0) {
- circleChecker = 1;
- switch (winnerSave) {
- case 0: break;
- case 2: if (currentStep == 6) squareScoreMustWin[y]++; scoreBuffer++; break;
- default: break;
- }
- }
- if (squareSave[u] == '.') squareSave[u] = 'X'; else break;
- winCheckSave();
- if (winnerSave != 0 && circleChecker == 0) {
- switch (winnerSave) {
- case 0: break;
- case 1: if (currentStep == 6) squareScoreMustLose[u]++; scoreBuffer--; break;
- default: break;
- }
- }
- if (squareSave[i] == '.') squareSave[i] = 'O'; else break;
- winCheckSave();
- if (winnerSave != 0 && circleChecker == 0) {
- switch (winnerSave) {
- case 0: break;
- case 2: if (currentStep == 8) squareScoreMustWin[i]++; scoreBuffer++; break;
- default: break;
- }
- }
- if (squareSave[o] == '.') squareSave[o] = 'X'; else break;
- winCheckSave();
- if (winnerSave != 0 && circleChecker == 0) {
- circleChecker = 1;
- switch (winnerSave) {
- case 0: break;
- case 1: if (currentStep == 8) squareScoreMustLose[o]++; scoreBuffer--; break;
- default: break;
- }
- }
- winCheckSave();
- switch (currentStep) {
- case 2: squareScore[w] = scoreBuffer + squareScore[w]; break;
- case 4: squareScore[r] = scoreBuffer + squareScore[r]; break;
- case 6: squareScore[y] = scoreBuffer + squareScore[y]; break;
- case 8: squareScore[i] = scoreBuffer + squareScore[i]; break;
- default: break;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- int minScore = 999999;
- for (int i = 0; i != 9; i++) {
- if (square[i] == '.') {
- printf("\nScore for square[%d] score: %d | mustLose: %d | mustWin: %d", i + 1, squareScore[i], squareScoreMustLose[i], squareScoreMustWin[i]);
- }
- if (i == 0) {
- minScore = squareScore[i];
- }
- else {
- if (squareScore[i] < minScore) {
- minScore = squareScore[i];
- }
- }
- }
- for (int i = 0; i != 9; i++) {
- if (squareScore[i] > minScore && square[i] == '.') {
- if (square[i] == '.') {
- minScore = squareScore[i];
- aiPick = i;
- }
- }
- int randomCount = rand() % 2;
- if (squareScore[i] == minScore && randomCount == 1 && square[i] == '.') {
- if (square[i] == '.') {
- minScore = squareScore[i];
- aiPick = i;
- }
- }
- }
- int checkerMust = 0;
- for (int i = 0; i != 9; i++) {
- if (squareScoreMustWin[i] > 0 && square[i] == '.') {
- aiPick = i;
- checkerMust = 1;
- }
- }
- for (int i = 0; i != 9; i++) {
- if (squareScoreMustLose[i] > 0 && checkerMust == 0 && square[i] == '.') {
- aiPick = i;
- checkerMust = 1;
- }
- }
- if (currentStep == 2 && square[4] != '.') {
- int pickRand;
- pickRand = rand() % 4;
- if (pickRand == 0) aiPick = 0;
- if (pickRand == 1) aiPick = 2;
- if (pickRand == 2) aiPick = 6;
- if (pickRand == 3) aiPick = 8;
- }
- if (currentStep == 2) {
- if (square[0] != '.' || square[2] != '.' || square[6] != '.' || square[8] != '.') aiPick = 4;
- }
- if (currentStep == 4) {
- if (square[0] == 'X' && square[8] == 'X' || square[2] == 'X' && square[6] == 'X') {
- int pickRand;
- pickRand = rand() % 4;
- if (pickRand == 0) aiPick = 1;
- if (pickRand == 1) aiPick = 3;
- if (pickRand == 2) aiPick = 5;
- if (pickRand == 3) aiPick = 7;
- }
- }
- printf("\nBot picked %d\n", aiPick + 1);
- square[aiPick] = ai;
- chosen[currentStep - 1] = aiPick;
- }
- int main() {
- int gameChecker = 1;
- do {
- playerChoise();
- set0();
- for (int q = 1; q != 10; q++) {
- currentStep = q;
- winCheck();
- if (winner != 0) break;
- if (player == 'X') {
- if (q % 2 == 1) {
- if (currentStep == 1) {
- outPut();
- playerStep();
- outPut();
- }
- else {
- playerStep();
- outPut();
- }
- }
- else {
- aiStepO();
- outPut();
- }
- }
- else {
- if (q % 2 == 1) {
- if (q == 1) {
- square[4] = 'X';
- chosen[0] = 4;
- outPut();
- }
- else {
- aiStepX();
- outPut();
- }
- }
- else {
- playerStep();
- }
- }
- }
- winCheck();
- if (winner == 0) printf("\nFinished as Draw!\n");
- else {
- if (winner == 1) {
- printf("\nFinished, X is winner!");
- }
- else {
- printf("\nFinished, O is winner!");
- }
- }
- int checkerG = 0;
- printf("\nDo you want to play again? 1 - YES; 0 - NO: ");
- do {
- int res;
- do {
- res = scanf("%d", &gameChecker);
- while (getchar() != '\n');
- if (res != 1) {
- printf("\nDo you want to play again? 1 - YES; 0 - NO: ");
- }
- } while (res != 1);
- if (gameChecker == 0 || gameChecker == 1) checkerG = 1;
- else {
- printf("\nDo you want to play again? 1 - YES; 0 - NO: ");
- }
- } while (checkerG == 0);
- } while (gameChecker != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement