Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <time.h>
- #include <stdlib.h>
- #include <windows.h>
- using namespace std;
- int table[3][3];
- string drawXO[3][3];
- int xplayer,yplayer;
- int xcomp,ycomp;
- int xy;
- int srand1;
- bool isFull;
- bool playerwin,compwin;
- char restart;
- void computerplace();
- void drawtable();
- void playerplace();
- void showdata();
- void randomizing();
- void exiting();
- void tableIsFull();
- void checktable();
- void initialiseDrawXO();
- void checkanddraw();
- void checkwin();
- void gameloop();
- int main()
- {
- srand(time(NULL));
- initialiseDrawXO();
- gameloop();
- return 0;
- }
- void gameloop(){
- srand(time(NULL));
- initialiseDrawXO();
- while((isFull == false) && (playerwin == false) && (compwin == false)){
- srand1 = rand() % 100;
- checkanddraw();
- randomizing();
- playerplace();
- tableIsFull();
- if (isFull == true){
- checkwin();
- break;
- }
- computerplace();
- checkwin();
- }
- checkanddraw();
- if (playerwin == true){
- cout << "You Won!" << endl;
- }else if(compwin == true){
- cout << "You Lost!" << endl;
- }else{
- cout << "Draw!" << endl;
- }
- }
- void checkwin(){
- HANDLE hConsole;
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- if((table[0][2] == 1) && (table[1][1] == 1) && (table[2][0] == 1)){
- playerwin = true;
- }else if((table[0][2] == 2) && (table[1][1] == 2) && (table[2][0] == 2)){
- compwin = true;
- }
- if((table[0][0] == 1) && (table[1][1] == 1) && (table[2][2] == 1)){
- playerwin = true;
- }else if((table[0][0] == 2) && (table[1][1] == 2) && (table[2][2] == 2)){
- compwin = true;
- }
- if((table[0][0] == 1) && (table[0][1] == 1) && (table[0][2] == 1)){
- playerwin = true;
- }else if((table[0][0] == 2) && (table[0][1] == 2) && (table[0][2] == 2)){
- compwin = true;
- }
- if((table[1][0] == 1) && (table[1][1] == 1) && (table[1][2] == 1)){
- playerwin = true;
- }else if((table[1][0] == 2) && (table[1][1] == 2) && (table[1][2] == 2)){
- compwin = true;
- }
- if((table[2][0] == 1) && (table[2][1] == 1) && (table[2][2] == 1)){
- playerwin = true;
- }else if((table[2][0] == 2) && (table[2][1] == 2) && (table[2][2] == 2)){
- compwin = true;
- }
- if((table[0][0] == 1) && (table[1][0] == 1) && (table[2][0] == 1)){
- playerwin = true;
- }else if((table[0][0] == 2) && (table[1][0] == 2) && (table[2][0] == 2)){
- compwin = true;
- }
- if((table[0][1] == 1) && (table[1][1] == 1) && (table[2][1] == 1)){
- playerwin = true;
- }else if((table[0][1] == 2) && (table[1][1] == 2) && (table[2][1] == 2)){
- compwin = true;
- }
- if((table[0][2] == 1) && (table[1][2] == 1) && (table[2][2] == 1)){
- playerwin = true;
- }else if((table[0][2] == 2) && (table[1][2] == 2) && (table[2][2] == 2)){
- compwin = true;
- }
- }
- void checkanddraw(){
- system("CLS");
- checktable();
- drawtable();
- }
- void checktable(){
- for(int v = 0; v <= 2; v++){
- for(int b = 0; b <= 2; b++){
- if (table[v][b] == 1){
- drawXO[v][b] = "X";
- }else if (table[v][b] == 2){
- drawXO[v][b] = "O";
- }
- }
- }
- }
- void initialiseDrawXO(){
- for (int z = 0; z <= 2; z++){
- for(int x = 0; x <= 2; x++){
- drawXO[z][x] = " ";
- }
- }
- }
- void tableIsFull(){
- int m,n,z;
- z = 0;
- for (m = 0; m <= 2; m++){
- for(n = 0; n <= 2; n++){
- if (table[m][n] == 0){
- isFull = false;
- z+= 1;
- break;
- }
- }
- if (table[m][n] == 0){
- break;
- }
- }
- if((m >= 2) && (n >= 2) && (z == 0)){
- isFull = true;
- }
- }
- void randomizing(){
- srand(srand1);
- xcomp = rand()%3;
- ycomp = rand()%3;
- }
- void computerplace(){
- srand1 += 1;
- randomizing();
- if(table[xcomp][ycomp] != 1 && table[xcomp][ycomp] != 2){
- table[xcomp][ycomp] = 2;
- }else{
- computerplace();
- }
- }
- void playerplace(){
- cout << "Your Turn." << endl;
- cout << "X/Y: ";
- cin >> xy;
- xplayer = xy / 10;
- yplayer = xy % 10;
- if (((xplayer >= 0 && xplayer <= 2) && (yplayer >= 0 && yplayer <= 2)) && ((table[xplayer][yplayer] != 1 && table[xplayer][yplayer] != 2))){
- table[xplayer][yplayer] = 1;
- }else{
- cout << "Choose another position!" << endl;
- _sleep(1000);
- system("CLS");
- drawtable();
- playerplace();
- }
- }
- void showdata(){
- for (int z = 0 ; z <= 2 ; z++){
- for(int c = 0 ; c <= 2; c++){
- cout << "table[" << z << "][" << c << "] = " << table[z][c] << endl;
- }
- }
- cout << endl;
- }
- void drawtable(){
- HANDLE hConsole;
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- cout << " 0 1 2";
- cout << endl;
- cout << endl;
- cout << "0 " << drawXO[0][0]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[0][1]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[0][2] << " " << endl;
- SetConsoleTextAttribute(hConsole,11);
- cout << " - - - - - " << endl;
- SetConsoleTextAttribute(hConsole,7);
- cout << "1 " << drawXO[1][0]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[1][1]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[1][2] << " " << endl;
- SetConsoleTextAttribute(hConsole,11);
- cout << " - - - - - " << endl;
- SetConsoleTextAttribute(hConsole,7);
- cout << "2 " << drawXO[2][0]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[2][1]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[2][2] << " " << endl;
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement