1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
Trying to make a Tic-Tac-Toe engine with board.
2 player mode works just fine, and 1 player mode with computer choosing randomly also works well.
But now I'm trying to make so that if there's danger of the user winning, the computer will block.
The first time there's danger, it will block, but if it happens a second time, it does n]othing.
Also if there's danger of user winning but the computer already wrote in that square, it will still try to write in that square, which results in nothing happening.
It's probably because of some stupid error, but I can't find it.
[code
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctime>
#include <stdlib.h>
using namespace std;
int main(){
//square: The square that the user picks.
//win: When this turns into 1, the game ends.
//change: Determining if it's X's turn or O's turn. (0 = X, 1 = Y)
/* X[10], Y[10]: 9 arrays for remembering which squares are already filled in.
Put in 11 just in case, X[1] - First square taken by X etc.*/
//tie: Determining if all squares are filled in, if it is, print DRAW!!.
//random: Computer's random number.
int square, win, change, X[10], O[10], tie[10], random;
srand (time(0));
random = rand() % 9 + 1;
X[5] = 0;
O[5] = 0;
//board: The Tic-Tac-Toe board template. Also changes for each square that's filled in.
//temp, temp2: temp first copies board, then cuts out what it needs depending on which square it's filling, then it adds in "XXX" or "OOO" depending on whose turn it is. temp2 then copies board and erases the part that has been changed. Finally, board turns into temp + temp2.
string board, temp, temp2;
board = " | | \n-------------\n | | \n-------------\n | | ";
cout << board << endl;
//Checking if X has 3 X's in a row.
while(win != 1){
if((X[1] == 1 && X[4] == 1 && X[7] == 1 )|| (X[2] == 1 && X[5] == 1 && X[8] == 1 )|| (X[3] == 1 && X[6] == 1 && X[9] == 1 )|| (X[1] == 1 && X[2] == 1 && X[3] == 1 )|| (X[4] == 1 && X[5] == 1 && X[6] == 1 )|| (X[7] == 1 && X[8] == 1 && X[9] == 1 )|| (X[1] == 1 && X[5] == 1 && X[9] == 1) || (X[3] == 1 && X[5] == 1 && X[7] == 1)){
cout << "\nX WINS!!!" << endl;
return 0;
}
//Checking if O has 3 O's in a row.
else if((O[1] == 1 && O[4] == 1 && O[7] == 1) || (O[2] == 1 && O[5] == 1 && O[8] == 1) || (O[3] == 1 && O[6] == 1 && O[9] == 1 )|| (O[1] == 1 && O[2] == 1 && O[3] == 1 )|| (O[4] == 1 && O[5] == 1 && O[6] == 1 )|| (O[7] == 1 && O[8] == 1 && O[9] == 1) || (O[1] == 1 && O[5] == 1 && O[9] == 1 )|| (O[3] == 1 && O[5] == 1 && O[7] == 1)){
cout << "\nO WINS!!!" << endl;
return 0;
}
//If all squares are filled, and no one has a row, print DRAW!!
else if(tie[1] == 1 && tie[2] == 1 && tie[3] == 1 && tie[4] == 1 && tie[5] == 1 && tie[6] == 1 && tie[7] == 1 && tie[8] == 1 && tie[9] == 1){
cout << "\nDRAW!!!" << endl;
return 0;
}
/*The user enters a number between 1 ~ 9. If the square it represents:
1: Top left square
2: Top middle square
3: Top right square
etc.
is taken, then tell the user so. It also checks for numbers that are not within that range.*/
if(change == 0){
cout << "Which square?" << endl;
cin >> square;
if(square == 1){
if(X[1] == 1 || O[1] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
else if(square == 2){
if(X[2] == 1 || O[2] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
else if(square == 3){
if(X[3] == 1 || O[3] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
else if(square == 4){
if(X[4] == 1 || O[4] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
else if(square == 5){
if(X[5] == 1 || O[5] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
else if(square == 6){
if(X[6] == 1 || O[6] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
else if(square == 7){
if(X[7] == 1 || O[7] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
else if(square == 8){
if(X[8] == 1 || O[8] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
else if(square == 9){
if(X[9] == 1 || O[9] == 1){
cout << "Square Already Taken!\n" << endl;
continue;
}
}
if(square < 1 || square > 9){
cout << "Invalid Number!" << endl;
continue;
}
|