//Tic Tac Toe App
//premier-gamer
//header include
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
//using namespace
using namespace System;
//initial grid
int grid[3][3];
int pickPlayer()
{
return rand() % 2 + 1;
}
void resetScore()
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
grid[i][j] = 0;
}
}
}
void drawGrid()
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
String^ ch;
switch (grid[i][j])
{
case 0 : ch = "-"; break;
case 1 : ch = "X"; break;
case 2 : ch = "O";
}
Console::Write( "\t" + ch );
}
Console::Write("\n\n");
}
}
array<int>^ countOwns(int player)
{
int size = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if(grid[i][j] == player)
size++;
int index = 0;
array<int>^ owns = gcnew array<int>(size);
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if(grid[i][j] == player)
{
owns[index] = ((i*3) + j)+1;
index++;
}
}
}
return owns;
}
int magicSquare( int x )
{
switch(x)
{
case 1: return 8;
case 2: return 1;
case 3: return 6;
case 4: return 3;
case 5: return 5;
case 6: return 7;
case 7: return 4;
case 8: return 9;
case 9: return 2;
}
}
bool checkWinner(int player)
{
bool win = false;
array<int>^ owns = countOwns(player);
for(int x=0;x<owns->Length;x++)
{
for(int y=(x+1);y<owns->Length;y++)
{
for(int z=(y+1);z<owns->Length;z++)
{
//this algorithm sucks donkey balls...
if( (magicSquare(owns[x]) + magicSquare(owns[y]) + magicSquare(owns[z]) == 15) && owns->Length > 2)
win = true;
}
}
}
return win;
}
void showHelpScreen()
{
Console::Clear();
Console::Write("\t================================\n\t= Tic Tac Toe =\n\t= Made By: PREMIER-GAMER =\n\t= =\n\t= -= INSTRUCTIONS =- =\n\t= 1. Enter a number (1-9) =\n\t= 2. Switch Characters =\n\t= 3. Try and get 3 in a row =\n\t================================");
Console::WriteLine("\n\n\tPress any key to return.");
Console::ReadKey();
Console::Clear();
}
void showMainMenu()
{
Console::WriteLine("\n\n\t\tX\tO\tX\n\n\t\tO\tX\t-\n\n\t\tO\t-\tX\n\n\n\t\t1. Start Game\n\t\t2. Help\n\t\t3. Exit");
char selection = Console::ReadKey().KeyChar;
switch(selection)
{
case '1': break;
case '2': showHelpScreen();Console::Clear();showMainMenu(); break;
case '3': Environment::Exit(0); break;
default: Console::Clear(); showMainMenu();
}
Console::Clear();
}
int main()
{
Console::Title = "Tic Tac Toe";
srand ( time(NULL) );
bool gameStart = true;
bool firstRun = true;
int number,row,ovr;
int player = pickPlayer();
resetScore();
while ( gameStart )
{
if(firstRun)
{
firstRun = false;
showMainMenu();
}
drawGrid();
Console::WriteLine("\n\nCurrent Player: " + player + "\nSelect a spot to choose. (1-9)\n\nType \"help\" for more information.\n\n");
String^ input = Console::ReadLine(); //get this incase of help.
if(input == "help")
{
showHelpScreen();
drawGrid();
}
else
{
try{number = Convert::ToInt32(input);}
catch(Exception^ ex){number = 12;}
}
if(number > 0 && number < 10)
{
row = System::Math::Floor(number/3);
ovr = number-(3*row)-1;
}
else
{
Console::Clear();
continue;
}
if(grid[row][ovr] == 0)
{
grid[row][ovr] = player;
if(checkWinner(player))
{
Console::Clear();
Console::WriteLine(L"Player " + player + " is the winner!\n\nWould you like to play again? (y/n)");
char input = Console::ReadKey().KeyChar;
if(input == 'y'
{
Console::Clear();
resetScore();
player = pickPlayer();
continue;
}
else
{
Console::Clear();
showMainMenu();
}
}
else
{
if(countOwns(player)->Length >= 5)
{
Console::Clear();
Console::WriteLine(L"Looks like it's a tie!\n\nWould you like to play again? (y/n)");
char input = Console::ReadKey().KeyChar;
if(input == 'y'
{
Console::Clear();
resetScore();
player = pickPlayer();
continue;
}
else
{
Console::Clear();
showMainMenu();
}
}
}
if(player == 1)
player++;
else
player--;
}
else
{
Console::Clear();
continue;
}
Console::Clear();
}
return 0;
}
Copyright © 2024, NextGenUpdate.
All Rights Reserved.