Main.cpp
Code:
// Orx Game.cpp : Defines the entry point for the console application.
//Written By Lauri Orgla a.k.a OrX
//Tic-Tac-Toe game
#include "stdafx.h"
#include "GameFrame.h"
int main(){
system("title OrX - TicTacToe 2011");
MFrame NewGame;
NewGame.SetUp();
NewGame.GameAbout();
NewGame.SelectName();
NewGame.StartKeybd();
while(NewGame.GAME = true){
NewGame.Draw();
NewGame.GameState();
NewGame.GetMove();
NewGame.AIMove();
}
return 0;
}
GameFrame.h
Code:
#include <iostream>
#include <windows.h>
#include <string>
class MFrame {
private:
char T[9];
int f[9]; // fields
int a[9]; // allocated
public:
char PlayerName[64];
static int CheckKeys();
bool DisplayAbout;
bool GAME;
bool IsSlotAvailible(int slotID);
void SetUp();
void ReStart();
void Draw();
void AIMove();
void ExitGame();
void SelectName();
void StartKeybd();
void GameAbout();
int CheckForWin();
int GameState();
int MakeMove(int Move);
int GetMove();
};
void MFrame::Draw (){
int temp = 0;
while(temp <= 9){
if(a[temp] == 1){T[temp] = 'X';}
if(a[temp] == 2){T[temp] = 'O';}
if(a[temp] == 0){T[temp] = (char)(((int)'0')+temp);}
temp++;
}
system("CLS");
if(DisplayAbout == true){
MFrame::GameAbout();
}
std::cout << T[0] << " | " << T[1] << " | " << T[2] << std::endl;
std::cout <<"- + - + -" << std::endl;
std::cout << T[3] << " | " << T[4] << " | " << T[5] << std::endl;
std::cout <<"- + - + -" << std::endl;
std::cout << T[6] << " | " << T[7] << " | " << T[8] << std::endl;
system("color c");
Sleep(400);
system("color f");
Sleep(100);
}
void MFrame::SetUp (){
MFrame::ReStart();
GAME = true;
DisplayAbout = true;
}
void MFrame::ReStart(){
//note allocation status 1 = X and 2 = O
f[0] = 1;
f[1] = 2;
f[2] = 3;
f[3] = 4;
f[4] = 5;
f[5] = 6;
f[6] = 7;
f[7] = 8;
f[8] = 9;
//fields
a[0] = 0;
a[1] = 0;
a[2] = 0;
a[3] = 0;
a[4] = 0;
a[5] = 0;
a[6] = 0;
a[7] = 0;
a[8] = 0;
}
bool MFrame::IsSlotAvailible(int slotID){
if(a[slotID] == 0){
return true;
}
return false;
}
int MFrame::GetMove(){
int move = 0;
std::cout << std::endl << "Pick a number: ";
std::cin >> move;
if(move == 99){
MFrame::DisplayAbout = false;
Draw();
GetMove();
}
if(move >= 0){
if(move <= 9){
if(IsSlotAvailible(move) == true){
return MakeMove(move);
}
}
}
std::cout << std::endl << "You cant choose this number ! try another one " ;
GetMove();
}
int MFrame::MakeMove(int move){
// 1 is player move, 2 is AI
a[move] = 1;
return 1;
}
void MFrame::AIMove(){
int random_integer = 0;
while(1){
// 0 4 8
if(a[0] == 1 && a[8] == 1 && a[4] == 0){a[4] = 2; break;}
if(a[0] == 0 && a[8] == 1 && a[4] == 1){a[0] = 2; break;}
if(a[0] == 1 && a[8] == 0 && a[4] == 1){a[8] = 2; break;}
// 2 4 6
if(a[2] == 0 && a[4] == 1 && a[6] == 1){a[2] = 2; break;}
if(a[2] == 1 && a[4] == 0 && a[6] == 1){a[4] = 2; break;}
if(a[2] == 1 && a[4] == 1 && a[6] == 0){a[6] = 2; break;}
// 0 1 2
if(a[0] == 1 && a[1] == 1 && a[2] == 0){a[2] = 2; break;}
if(a[0] == 1 && a[1] == 0 && a[2] == 1){a[1] = 2; break;}
if(a[0] == 0 && a[1] == 1 && a[2] == 1){a[0] = 2; break;}
// 3 4 5
if(a[3] == 0 && a[4] == 1 && a[5] == 1){a[3] = 2; break;}
if(a[3] == 1 && a[4] == 0 && a[5] == 1){a[4] = 2; break;}
if(a[3] == 1 && a[4] == 1 && a[5] == 0){a[5] = 2; break;}
//6 7 8
if(a[6] == 0 && a[7] == 1 && a[8] == 1){a[6] = 2; break;}
if(a[6] == 1 && a[7] == 0 && a[8] == 1){a[7] = 2; break;}
if(a[6] == 1 && a[7] == 1 && a[8] == 0){a[8] = 2; break;}
//0 3 6
if(a[0] == 0 && a[3] == 1 && a[6] == 1){a[0] = 2; break;}
if(a[0] == 1 && a[3] == 0 && a[6] == 1){a[3] = 2; break;}
if(a[0] == 1 && a[3] == 1 && a[6] == 0){a[6] = 2; break;}
//1 4 7
if(a[1] == 0 && a[4] == 1 && a[7] == 1){a[1] = 2; break;}
if(a[1] == 1 && a[4] == 0 && a[7] == 1){a[4] = 2; break;}
if(a[1] == 1 && a[4] == 1 && a[7] == 0){a[7] = 2; break;}
//2 5 8
if(a[2] == 0 && a[5] == 1 && a[8] == 1){a[2] = 2; break;}
if(a[2] == 1 && a[5] == 0 && a[8] == 1){a[5] = 2; break;}
if(a[2] == 1 && a[5] == 1 && a[8] == 0){a[8] = 2; break;}
//end of algorythm
//if algorythm fails, then random
random_integer = ((9 * rand()) / RAND_MAX);
if(IsSlotAvailible(random_integer) == true){
a[random_integer] = 2;
break;
}
if(a[0] != 0 && a[1] != 0 && a[2] != 0 && a[3] != 0 && a[4] != 0 && a[5] != 0 && a[6] != 0 && a[7] != 0 && a[8] != 0){
break;
}
}
}
int MFrame::CheckForWin(){
//lets check for player 1
int pl1 = 0, pl2 = 0;
if(a[0] == 1 && a[1] == 1 && a[2] == 1){pl1 = 1;}
if(a[3] == 1 && a[4] == 1 && a[5] == 1){pl1 = 1;}
if(a[6] == 1 && a[7] == 1 && a[8] == 1){pl1 = 1;}
if(a[0] == 1 && a[3] == 1 && a[6] == 1){pl1 = 1;}
if(a[1] == 1 && a[4] == 1 && a[7] == 1){pl1 = 1;}
if(a[2] == 1 && a[5] == 1 && a[8] == 1){pl1 = 1;}
if(a[0] == 1 && a[4] == 1 && a[8] == 1){pl1 = 1;}
if(a[2] == 1 && a[4] == 1 && a[6] == 1){pl1 = 1;}
if(a[0] == 2 && a[1] == 2 && a[2] == 2){pl2 = 1;}
if(a[3] == 2 && a[4] == 2 && a[5] == 2){pl2 = 1;}
if(a[6] == 2 && a[7] == 2 && a[8] == 2){pl2 = 1;}
if(a[0] == 2 && a[3] == 2 && a[6] == 2){pl2 = 1;}
if(a[1] == 2 && a[4] == 2 && a[7] == 2){pl2 = 1;}
if(a[2] == 2 && a[5] == 2 && a[8] == 2){pl2 = 1;}
if(a[0] == 2 && a[4] == 2 && a[8] == 2){pl2 = 1;}
if(a[2] == 2 && a[4] == 2 && a[6] == 2){pl2 = 1;}
if(pl2 == 1 && pl1 == 1){return 3;}
if(pl1 == 1){return 1;}
if(pl2 == 1){return 2;}
if(a[0] != 0 && a[1] != 0 && a[2] != 0 && a[3] != 0 && a[4] != 0 && a[5] != 0 && a[6] != 0 && a[7] != 0 && a[8] != 0){return 3;}
return 0;
}
int MFrame::GameState(){
int xz= 0;
switch(CheckForWin()){
case 1:
std::cout << "Player has won the game! " << std::endl;
while(xz < 10){
system("color a");
Sleep(50);
system("color b");
Sleep(50);
system("color d");
Sleep(50);
system("color e");
Sleep(50);
system("color f");
xz++;
}
ExitGame();
break;
case 2:
std::cout << "PC has won the game! " << std::endl;
system("color a");
Sleep(50);
system("color b");
Sleep(50);
system("color f");
ExitGame();
break;
case 3:
std::cout << "Seems that we have a TIE! " << std::endl;
while(xz < 10){
system("color e");
Sleep(50);
system("color f");
xz++;
}
ExitGame();
break;
}
return 0;
}
void MFrame::ExitGame(){
char tmp;
std::cout << "Would you like to play more? y/n :";
std::cin >> tmp;
if(tmp == 'n'){
GAME = false;
exit(1);
}
else
{
ReStart();
Draw();
}
}
void MFrame::SelectName(){
std::cout << " Please insert your name: ";
std::cin >> PlayerName;
}
void MFrame::StartKeybd(){
CreateThread(0, 0 , (LPTHREAD_START_ROUTINE)CheckKeys, 0,0,0);
}
int MFrame::CheckKeys(){
while(1){
if(GetAsyncKeyState(VK_ESCAPE)){
MessageBoxA(0, "Good Bye!", "Tic-Tac-Toe 2011 OrX", 0);
exit(1);
}
if(GetAsyncKeyState(VK_F8) &1){
// DisplayAbout = false;
// Draw();
}
}
return 0;
}
void MFrame::GameAbout(){
std::cout << "This game is written by Lauri Orgla a.k.a OrX" << std::endl;
std::cout << "This version of game is improved, with harder AI (Artificial intelligence)" << std::endl;
std::cout << "Report any bugs @ TheOrX@hotmail.com" << std::endl;
std::cout << "Type 99 to the 'Pick a number' to remove this message." << std::endl;
std::cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " << std::endl;
}
Test it out, and tell me how it went its definetly better than the first version!
I might start learning how to use winsock via c++ soon, i might make it online. just for learning purposes
Cheers