Tic-Tac-Toe Basic Console Game Log
I've recently started taking classes at North Western on C++/C programming and this is round 1 with Tic-Tac-Toe basic Console game.
I've made this is Microsoft Visual Studio Academic Edition. I've been taking classes regularly and am learning a large amount on this coding language. I wrote this up in less than 2 hours.
This is my very first attempt on Console Based Games and I'll be updating this constantly as it's not working as it should.
All it's currently doing is recreating the 6x6 grid [though it only seems to be a 3x3 grid] and every time shows the place where you moved, the the computer finds the next open spot in the grid and fills that.
You however, can pick any spot in the 3x3 grid. The computer has no mental capability of it's own to date, and should be updated in the next few days based on my schedule.
Enjoy
Please, rate the current code /10 and give feedback. IF you don't have any comment on my coding it will be considered spam and reported.
I've recently started taking classes at North Western on C++/C programming and this is round 1 with Tic-Tac-Toe basic Console game.
I've made this is Microsoft Visual Studio Academic Edition. I've been taking classes regularly and am learning a large amount on this coding language. I wrote this up in less than 2 hours.
This is my very first attempt on Console Based Games and I'll be updating this constantly as it's not working as it should.
All it's currently doing is recreating the 6x6 grid [though it only seems to be a 3x3 grid] and every time shows the place where you moved, the the computer finds the next open spot in the grid and fills that.
You however, can pick any spot in the 3x3 grid. The computer has no mental capability of it's own to date, and should be updated in the next few days based on my schedule.
Enjoy
Please, rate the current code /10 and give feedback. IF you don't have any comment on my coding it will be considered spam and reported.
Code:
// Attempt 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#define XWINS 1
#define OWINS 2
#define TIE 3
using namespace std;
void Draw_Board();
int DidAnyoneWin();
int AnySpotsLeft();
int MyTurn(char);
// Define 6 x 6 Grid and insert symbols in the array
char Board[][8] = {" 1 2 3 ", "1 | | ", " - - - ", "2 | | ", " - - - ", "3 | | "};
int _tmain(int argc, _TCHAR* argv[])
{
int i, j, Row=0, Column=0;
char XorO = NULL;
char other = NULL;
int done = false;
// Initialize grid
for (i=1; i<6; i+=2)
for (j=1; j<6; j+=2)
Board[i][j]=NULL;
// Take credit
cout << "Tic Tac Toe - By xaixiax Vanack\n" << endl << endl;
//Print Board
Draw_Board();
do
{
// Ask whether they are X's or O's
cout << "Are you X or O?\n";
cin >> XorO;
cout << endl << endl;
// Capitalize, if necessary
if (XorO == 'x')
XorO = 'X';
else if (XorO == 'o')
XorO = 'O';
} while (XorO != 'X' && XorO != 'O'); // make sure they only use X's and O's
// What am I?
if (XorO == 'X') other = 'O';
else other = 'X';
do
{
// Ask where they want theirs to go
cout << "What row do you want to place your piece in?\n";
cin >> Row;
cout << endl;
cout << "What column do you want to place your piece in?\n" << endl;
cin >> Column;
cout << endl;
// Compensate for Board layout
Row = 2 * Row -1;
Column = 2 * Column -1;
// Make sure space not already taken
if (Board[Row][Column]== 'X' || Board[Row][Column]== 'O')
{
cout << endl << " That spot is taken. You're a cheater so I win.\n" << endl << endl;
break;
} else Board[Row][Column]=XorO;
// Draw # w new mark
Draw_Board();
// Check for 3 in row , if so => done
done = DidAnyoneWin();
// If no empty spot => tie + done
if (!done) done = AnySpotsLeft();
if (!done)
{
// Place your mark in empty spot
MyTurn(other);
// If no empty spot => tie + done
done = AnySpotsLeft();
// Draw # w new mark
Draw_Board();
// Check for 3 in row or tie
done = DidAnyoneWin();
}
} while (!done);
switch (done)
{
case XWINS:
if (XorO == 'X')
cout << "Darn! You beat me!\n\n";
else cout << "In your face, I win!\n\n";
break;
case OWINS:
if (XorO == 'O')
cout << "Darn! You beat me!\n\n";
else cout << "In your face, I win!\n\n";
break;
case TIE:
cout << "Yuck! This is like kissing your sister!\n\n";
break;
default:
cout << "Game over ... Cheater.\n" << endl;
}
cout << "\n\nHit enter to finish\n" << endl;
cin >> Column;
return 0;
}
void Draw_Board()
{
int i=0, j=0;
cout << endl;
for (i=0; i<6; i++)
{
cout << " ";
for (j=0; j<6; j++)
cout << Board[i][j];
cout << endl;
}
cout << endl;
}
int DidAnyoneWin()
{
int row=0, column=0;
int winner = 0;
// Any horizontal winners?
column = 1;
for (row=1; row<6; row+=2)
if (Board[row][column]== Board[row][column+2] && Board[row][column+2]== Board[row][column+4]
&& Board[row][column]!= NULL)
if (Board[row][column]== 'X')
winner=XWINS;
else winner=OWINS;
// Any vertical winners?
row = 1;
for (column=1; column<6; column+=2)
if (Board[row][column]== Board[row+2][column] && Board[row+2][column]== Board[row+4][column]
&& Board[row][column]!= NULL)
if (Board[row][column]== 'X')
winner=XWINS;
else winner=OWINS;
// Check descending vertical
row = 1;
column = 1;
if (Board[row][column]== Board[row+2][column+2] && Board[row+2][column+2]== Board[row+4][column+4]
&& Board[row][column]!= NULL)
if (Board[row][column]== 'X')
winner=XWINS;
else winner=OWINS;
// Check ascending vertical
row = 5;
column = 1;
if (Board[row][column]== Board[row-2][column+2] && Board[row-2][column+2]== Board[row-4][column+4]
&& Board[row][column]!= NULL)
if (Board[row][column]== 'X')
winner=XWINS;
else winner=OWINS;
return winner;
}
int AnySpotsLeft()
{
int row=0, column=0;
int full = TIE;
// Is there an empty space?
for (row=1; row<6; row+=2)
{
for (column=1; row<6; row+=2)
if (Board[row][column]== NULL)
{
full = false;
break;
}
if (full = false) break;
}
return full;
}
MyTurn(char MyChar)
{
int row=0, column=0;
// find an empty space?
for (row=1; row<6; row+=2)
for (column=1; column<6; column+=2)
if (Board[row][column]== NULL)
{
Board[row][column]= MyChar;
row = 10;
break;
}
return 0;
}



