12 Replies - 6520 Views - Last Post: 07 April 2012 - 06:59 AM Rate Topic: -----

#1 Sharan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-April 12

Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 04:40 AM

Hey there, I want to make a game tic tac toe, for my university project,as I am first year student I don't know much about making a game. I made a game but it is not right as I need to make use of classes and algorithms, I kind of got an idea that I need a class for computer, player and grid, or class for nought , cross and grid, And I don't know how to do algorithms, while making the game.. I made this game, But this is not something I have been asked to do, well I can do this, but It won't get me good marks.... so can I edit this, or what do I need to do ,can someone please help me ?
and this is in c++
header file
#ifndef QUICKIO_H
#define QUICKIO_H

#include <conio.h>
#include <time.h>
#include <windows.h>
#include <cstdlib>

class QuickIO
{
	bool echo, extended;
	public:
	QuickIO(){echo = false;}
	void Echo() {echo = true;}
	void DontEcho() {echo = false;}
	bool isEchoing(){return echo;}
	void get(){if(echo) getche(); else getch();}
	QuickIO& operator >> (int& var);
	QuickIO& operator >> (char& var);
};

QuickIO& QuickIO::operator >> (int& var)
{
	do
		if(echo)
			var = getche();
		else
			var = getch();
	while(var==0||var==224);
	return *this;
}

QuickIO& QuickIO::operator >> (char& var)
{
	int p;
	do
		if(echo)
			p = getche();
		else
			p = getch();
	while(p==0||p==224);
	var = (char) p;
	return *this;
}

QuickIO qin;

class Stopwatch
{
	clock_t proctime, procelapsed;
	bool running;
public:
	Stopwatch(); //called automatically when a stopwatch object is created
	void start(); //starts the stopwatch
	float stop(); //stops the watch and returns the time
	void reset(); //resets the stopwatch to zero
	float read(); //returns the current time (without stopping the watch)
};


//implementation code...

Stopwatch::Stopwatch()
{
	procelapsed = 0;
	running = false;
}

void Stopwatch::start()
{
	proctime = clock();
	running = true;
}

float Stopwatch::stop()
{
	running = false;
	clock_t oldproc = proctime;
	proctime = clock(); 
	procelapsed = proctime - oldproc;
	return float(procelapsed)/CLOCKS_PER_SEC;
}

void Stopwatch::reset()
{
if(!running)
	procelapsed = 0;
}
 
float Stopwatch::read()
{
	if(running)
		return float(clock() - proctime + procelapsed)/CLOCKS_PER_SEC;
	else 
		return float(procelapsed)/CLOCKS_PER_SEC;
}

enum colour {Black, Blue, Green, Aqua, Red, Purple, Yellow, White};

class ColourController
{
	HANDLE hout;
	int foreground, background;
	void setConsole(){SetConsoleTextAttribute(hout, foreground + background*16);}
public:
	ColourController(): foreground(15), background(0) {hout = GetStdHandle(STD_OUTPUT_HANDLE);}
	void setForeground(colour c);
	void setBackground(colour c);
	void setForeBright(bool B)/>;
	void setBackBright(bool B)/>;
	void invert();
	~ColourController(){setForeground(White); setForeBright(true); setBackground(Black); setBackBright(false);}
};

void ColourController::setForeground(colour c)
{
if(c < 8)
foreground = c + (foreground >= 8) * 8;
setConsole();
}

void ColourController::setBackground(colour c)
{
if(c < 8)
background = c + (background >= 8) * 8;
setConsole();
}

void ColourController::setBackBright(bool B)/>
{
if(b && background < 8)
background += 8;
else if(!b && background >= 8)
background -= 8;
setConsole();
}

void ColourController::setForeBright(bool B)/>
{
if(b && foreground < 8)
foreground += 8;
else if(!b && foreground >= 8)
foreground -= 8;
setConsole();
}

void ColourController::invert()
{
foreground = foreground ^ background;
background = background ^ foreground;
foreground = foreground ^ background;
setConsole();
}

class CursorController
{
	HANDLE hout;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	COORD c;
	void setConsole(){SetConsoleCursorPosition(hout, c);}
public:
	CursorController(){hout = GetStdHandle (STD_OUTPUT_HANDLE);GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;}
	void setPosition(short x, short y){c.X = x; c.Y = y; setConsole();}
	short getX(){GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;return c.X;}
	short getY(){GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;return c.Y;}
	void clearAll();
};

void CursorController::clearAll()
{
	COORD origin = {0,0};
	if(!GetConsoleScreenBufferInfo(hout,&csbi))return;
	DWORD consize = csbi.dwSize.X * csbi.dwSize.Y;
	DWORD charsWritten;
	if(!FillConsoleOutputCharacter( hout,(TCHAR) ' ', consize, origin, &charsWritten ))return;
	if(!FillConsoleOutputAttribute( hout, csbi.wAttributes, consize, origin, &charsWritten ))return;
	SetConsoleCursorPosition(hout, origin);
}

class Random
{
	int mlower, mupper;
protected:
	int value;
public:
	Random():mlower(0),mupper(RAND_MAX){time_t seconds;time(&seconds);srand((unsigned int) seconds);}
	void setLimits(double lower, double upper){mlower=lower; mupper=upper;}
	int get();	
	int getLast(){return value;}
};
	
int Random::get()
{
	value = rand()%(mupper-mlower+1)+mlower;
	return value;
}

#endif

code:
#include<iostream>
#include<string>
#include"110ct.h"
using namespace std;

class TicTacToe 
{ 
private: 
	ColourController ct;
    ColourController cls; 
    CursorController crs;
    char Board[3][3]; 
    int play(); 
    void playermove(); 
    int turn(int); 
    int check (int, int); 
    void boardProgress(char x[][3]); 
    int Winner(char x[][3]); 
    int player1, player2, draw, player,winner,done; 
    int row, col; 
 
public: 
    int y, z, t, s; 
    void intBoard(); 
    int Statistics(int); 
 
    char playAgain(char); 
}playBall; 
 
int main() 
{ 
    playBall.y = 0,playBall.z = 0,playBall.t = 0,playBall.s = 0; 
    int Winner = 0; 
	ColourController ct;
    ColourController cls; 
    cls.setForeground(Aqua);
        cout << " welcome to TIC TAC TOE" <<endl; 
 
    char done = false; 
 
    while (!done) 
        { 
        ColourController cls; 
        cls.setForeground(Aqua);
        cout << " Good Luck!"<< endl;
                 
 
        
        playBall.intBoard(); 
 
    
    done = playBall.playAgain(done); 
} 
 

cout << "Thank you for my playing my game"<< endl; 
system ("pause"); 
return 0; 
} 
void TicTacToe::intBoard() 
{ 
    done = false; 
    winner = 0; 
    player = 2; 
    player1 = 1; 
    player2 = 2; 
    draw = 3; 
    int i,j; 
    for (j=0;j<3;j++) 
    { 
        for (i=0;i<3;i++) 
        { 
            Board[j][i]=0; 
        } 
    } 
    play(); 
} 
 
int TicTacToe::play() 
{ 
    int done = false; 
    boardProgress(Board); 
    while(!done) 
    { 
        playermove(); 
        boardProgress(Board); 
        done = Winner(Board); 
    } 
    Statistics(winner); 
    return 0; 
} 
void TicTacToe::playermove() 
{ 
    int answer = false; 
    while (answer == false) 
    { 
        cout <<"Row: "; 
        cin >> row; 
        row--; 
        cout <<"Column: "; 
        cin >> col; 
        col--; 
        answer = check(row,col); 
    } 
    player = turn(player); 
    if (player == 1) 
        Board[row][col] = 'X'; 
    else if (player == 2) 
        Board[row][col] = 'O'; 
    else 
        cout<< "Failed."; 
} 
int TicTacToe::check(int row, int col) 
{ 
    if(Board[row][col] == 0) 
    { 
        return true; 
    } 
    else if (Board[row][col] != 0) 
    { 
        cout <<"You can't go there"<< endl; 
        return false; 
    } 
    else 
    { 
        cout<< "Going through Check"; 
        Winner(Board); 
    } 
    return false; 
} 
 
int TicTacToe::turn(int player) 
{ 
    switch(player) 
    { 
    case 1: player = 2; 
        { 
        cout<<"It's Player 1's turn"<< endl; 
        break; 
        } 
    case 2: player = 1; 
        { 
        cout<<"It's Player 2's turn"<< endl; 
        break; 
        } 
    } 
    return player; 
} 
 
int TicTacToe::Winner(char x[][3]) 
{ 
    winner = 0; 
    int count = 0; 
    int a = row; 
    int b = col; 
    for (a=0;a<3;a++) 
    { 
        for (b=0;b<3;b++) 
        { 
            if (Board[a][b]==0) 
            { 
                count++; 
            } 
        } 
    } 
    if (count > 0) 
    { 
        int row, col, r, c, d, r1, c1, d1; 
        for (row=0; row<3; row++) 
        { 
            r=0; 
            r1=0; 
 
            for (col=0; col<3; col++) 
            { 
			
                if(x[row][col]=='X') 
                    r++; 
                if(x[row][col]=='O') 
                    r1++; 
                if (r1==3) 
                {  
                    winner=2; 
                } 
                if (r==3) 
                {    
                    winner=1; 
                } 
            } 
        } 
        for (col=0; col<3; col++) 
        { 
            r=0; 
            r1=0; 
            for (row=0; row<3; row++) 
            { 
                    if(x[row][col]=='X') 
                    r++; 
                if(x[row][col]=='O') 
                    r1++; 
                if (r1==3) 
                {    
                    winner=2; 
                } 
                if (r==3) 
                { 
                    winner=1; 
                    } 
            } 
        } 
        if (x[0][0]=='X' && x[1][1]=='X' && x[2][2]=='X') 
        { 
            winner=1; 
        } 
        else if (x[0][0]=='O' && x[1][1]=='O' && x[2][2]=='O') 
        { 
            winner=2; 
        } 
        else if (x[2][0]=='X' && x[1][1]=='X' && x[0][2]=='X') 
        { 
            winner=1; 
        } 
        else if (x[2][0]=='O' && x[1][1]=='O' && x[0][2]=='O') 
        { 
            winner=2; 
        } 
    } 
    else if (count == 9) 
    { 
        cout << "Its a draw" << endl; 
        winner = 3; 
    }    
    else 
    { 
        cout<< "next player's turn"<< endl; 
    } 
    if (winner > 0) 
    { 
        done = true; 
    } 
return done; 
} 
 
int TicTacToe::Statistics(int winner)     
{ 
    playBall.y++; 
    switch(winner) 
    { 
    case 1: z++; 
        { 
        break; 
        } 
    case 2: t++; 
        { 
        break; 
        } 
    case 3: s++; 
        { 
        break; 
        } 
    } 
    cout<<" Number of games you have played: " << y; 
    cout<<" Number of games Player 1 has won: " << z << " games.";  
    cout<<" Number of games Player 2 has won: " << t << " games."; 
    cout<<" Number have draws you had: " << s << " draws."; 
    return 0; 
} 
 
char TicTacToe::playAgain(char done) 
{ 
    cout <<"Would you like to play again"<< endl; 
    cout <<"Y/N: " << endl; 
    cin >> done; 
    if(done == 'Y' || done == 'y') 
    { 
         done = false; 
    } 
    else 
    { 
        done = true; 
    } 
    return done; 
} 
void TicTacToe::boardProgress(char x[][3]) 

{
	cls.setForeground(Purple);

    cout << "      |     |     \n"; 
    cout << "   " << x[0][0] << "  |  " << x[0][1] << "  |  " << x[0][2] <<"  \n"; 
    cout << " _____|_____|_____\n"; 
    cout << "      |     |     \n"; 
    cout << "   " << x[1][0] << "  |  " << x[1][1] << "  |  " << x[1][2] <<"  \n"; 
    cout << " _____|_____|_____\n"; 
    cout << "      |     |     \n"; 
    cout << "   " << x[2][0] << "  |  " << x[2][1] << "  | " << x[2][2] <<"  \n"; 
    cout << "      |     |     \n"; 
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe using classes and algorithms, c++ help please

#2 blackstab1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-April 12

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 05:33 AM

OMFG i just replyed really helpfully to this. fuck this comment system i just gave u the answer and took me to some rando page. serves me right for just joining 0_o.

Summary of my previous post,

Read
Beginning C++ Game Development

It will help you and i think i saw some mutated source of tic tac toe with an array of such to help with that subject. also 2 other things, dont triple post (noob much? you could get banned) and dont ask for homework. lucky im generous and its not too late. PM Me for book ifuwant
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 05:59 AM

Do you have a specific question, problem with your code?

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Sharan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-April 12

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 06:05 AM

No, I just need help, this code works, but how can I edit this and make classes instead
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 06:48 AM

You have a class and you are using it. What do you think is wrong with your current code? On change you can make is to stop using a global instance of your class:
class TicTacToe 
{ 

}playBall; // This playBall is a global instance of your class.
 


Instead create an instance of your in main:
int main()
{
   TicTacToe playBall;
   return 0;
}


Also you have a couple of instances of ColourController in your class but you never use them. Do you really need these instances in your class?


Jim
Was This Post Helpful? 0
  • +
  • -

#6 Sharan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-April 12

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 07:17 AM

Oh okay thanks, so I delete play ball from there and add to the main. and also, how can I get rid of player 2 and add computer instead?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 07:48 AM

One of the things you may want to rethink is illustrated in the following function:
void TicTacToe::intBoard() 
{ 
    done = false; 
    winner = 0; 
    player = 2; 
    player1 = 1; 
    player2 = 2; 
    draw = 3; 
    int i,j; 
    for (j=0;j<3;j++) 
    { 
        for (i=0;i<3;i++) 
        { 
            Board[j][i]=0; 
        } 
    } 
    play(); 
} 


Why is this function calling play()? In my opinion this function is doing too much. It should just initialize the board, nothing else. Why is this function even aware of any players winners, losers, it should just be clearing the board array.

Also in your playermove() function:
void TicTacToe::playermove() 
{ 
    int answer = false; 
    while (answer == false) 
    { 
        cout <<"Row: "; 
        cin >> row; 
        row--; 
        cout <<"Column: "; 
        cin >> col; 
        col--; 
        answer = check(row,col); 
    } 
    player = turn(player); 
    if (player == 1) 
        Board[row][col] = 'X'; 
    else if (player == 2) 
        Board[row][col] = 'O'; 
    else 
        cout<< "Failed."; 
} 


In my opinion this function is also doing too much. If you intend to use a computer as one of the players you will need at least two different functions, because the type of input will be different. Maybe turn() should be calling this function instead of being called by this function.

In my opinion you need to simplify most of your functions. Have most functions doing just one thing. If you want to be able to have two types of users you need to insure your user entry functions are isolated so you can call the correct function based on what type of players you have.

Also unless absolutely necessary you should try to not use any operating system specific code.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 Sharan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-April 12

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 08:00 AM

Thanks, But when I remove playerball, from the end of the class, it doesn't allow the player to start new game, why is that? But as I said my main problem is to have computer playing instead of a player so where I have player 2 can I replace it with the computer?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 08:25 AM

Quote

Thanks, But when I remove playerball, from the end of the class, it doesn't allow the player to start new game, why is that?


Post your code that illustrates this problem. Where and how are you creating your class instance?


Jim
Was This Post Helpful? 0
  • +
  • -

#10 Sharan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-April 12

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 11:10 AM

View Postjimblumberg, on 06 April 2012 - 08:25 AM, said:

Quote

Thanks, But when I remove playerball, from the end of the class, it doesn't allow the player to start new game, why is that?


Post your code that illustrates this problem. Where and how are you creating your class instance?


Jim

there isn't any option to close the discussion, what am I meant to do, as I have got another problem , and posting it here might be a problem?
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 06 April 2012 - 11:27 AM

If you are talking about the "question" you asked in this topic: help with sorting out TIC TAC TOE Code the question is about the same topic. Post the code and ask the question here.

Jim

This post has been edited by jimblumberg: 06 April 2012 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#12 Sharan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-April 12

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 07 April 2012 - 05:54 AM

View Postjimblumberg, on 06 April 2012 - 11:27 AM, said:

If you are talking about the "question" you asked in this topic: help with sorting out TIC TAC TOE Code the question is about the same topic. Post the code and ask the question here.

Jim

Okay thanks, my question is how can I change the board for the following code and have the board which is in the above code?
#include<iostream>
#include<string>
#include"110ct.h"
using namespace std;

class TicTacToe 
{ 
private: 
	ColourController ct;
    ColourController cls; 
    CursorController crs;
    char Board[3][3]; 
    int play(); 
    void playermove(); 
    int turn(int); 
    int check (int, int); 
    void boardProgress(char x[][3]); 
    int Winner(char x[][3]); 
    int player1, player2, draw, player,winner,done; 
    int row, col; 
 
public: 
    int y, z, t, s; 
    void intBoard(); 
    int Statistics(int); 
 
    char playAgain(char); 
}playBall; 
 
int main() 
{ 
    playBall.y = 0,playBall.z = 0,playBall.t = 0,playBall.s = 0; 
    int Winner = 0; 
	ColourController ct;
    ColourController cls; 
    cls.setForeground(Aqua);
        cout << " welcome to TIC TAC TOE" <<endl; 
 
    char done = false; 
 
    while (!done) 
        { 
        ColourController cls; 
        cls.setForeground(Aqua);
        cout << " Good Luck!"<< endl;
                 
 
        
        playBall.intBoard(); 
 
    
    done = playBall.playAgain(done); 
} 
 

cout << "Thank you for my playing my game"<< endl; 
system ("pause"); 
return 0; 
} 
void TicTacToe::intBoard() 
{ 
    done = false; 
    winner = 0; 
    player = 2; 
    player1 = 1; 
    player2 = 2; 
    draw = 3; 
    int i,j; 
    for (j=0;j<3;j++) 
    { 
        for (i=0;i<3;i++) 
        { 
            Board[j][i]=0; 
        } 
    } 
    play(); 
} 
 
int TicTacToe::play() 
{ 
    int done = false; 
    boardProgress(Board); 
    while(!done) 
    { 
        playermove(); 
        boardProgress(Board); 
        done = Winner(Board); 
    } 
    Statistics(winner); 
    return 0; 
} 
void TicTacToe::playermove() 
{ 
    int answer = false; 
    while (answer == false) 
    { 
        cout <<"Row: "; 
        cin >> row; 
        row--; 
        cout <<"Column: "; 
        cin >> col; 
        col--; 
        answer = check(row,col); 
    } 
    player = turn(player); 
    if (player == 1) 
        Board[row][col] = 'X'; 
    else if (player == 2) 
        Board[row][col] = 'O'; 
    else 
        cout<< "Failed."; 
} 
int TicTacToe::check(int row, int col) 
{ 
    if(Board[row][col] == 0) 
    { 
        return true; 
    } 
    else if (Board[row][col] != 0) 
    { 
        cout <<"You can't go there"<< endl; 
        return false; 
    } 
    else 
    { 
        cout<< "Going through Check"; 
        Winner(Board); 
    } 
    return false; 
} 
 
int TicTacToe::turn(int player) 
{ 
    switch(player) 
    { 
    case 1: player = 2; 
        { 
        cout<<"It's Player 1's turn"<< endl; 
        break; 
        } 
    case 2: player = 1; 
        { 
        cout<<"It's Player 2's turn"<< endl; 
        break; 
        } 
    } 
    return player; 
} 
 
int TicTacToe::Winner(char x[][3]) 
{ 
    winner = 0; 
    int count = 0; 
    int a = row; 
    int b = col; 
    for (a=0;a<3;a++) 
    { 
        for (b=0;b<3;b++) 
        { 
            if (Board[a][b]==0) 
            { 
                count++; 
            } 
        } 
    } 
    if (count > 0) 
    { 
        int row, col, r, c, d, r1, c1, d1; 
        for (row=0; row<3; row++) 
        { 
            r=0; 
            r1=0; 
 
            for (col=0; col<3; col++) 
            { 
			
                if(x[row][col]=='X') 
                    r++; 
                if(x[row][col]=='O') 
                    r1++; 
                if (r1==3) 
                {  
                    winner=2; 
                } 
                if (r==3) 
                {    
                    winner=1; 
                } 
            } 
        } 
        for (col=0; col<3; col++) 
        { 
            r=0; 
            r1=0; 
            for (row=0; row<3; row++) 
            { 
                    if(x[row][col]=='X') 
                    r++; 
                if(x[row][col]=='O') 
                    r1++; 
                if (r1==3) 
                {    
                    winner=2; 
                } 
                if (r==3) 
                { 
                    winner=1; 
                    } 
            } 
        } 
        if (x[0][0]=='X' && x[1][1]=='X' && x[2][2]=='X') 
        { 
            winner=1; 
        } 
        else if (x[0][0]=='O' && x[1][1]=='O' && x[2][2]=='O') 
        { 
            winner=2; 
        } 
        else if (x[2][0]=='X' && x[1][1]=='X' && x[0][2]=='X') 
        { 
            winner=1; 
        } 
        else if (x[2][0]=='O' && x[1][1]=='O' && x[0][2]=='O') 
        { 
            winner=2; 
        } 
    } 
    else if (count == 9) 
    { 
        cout << "Its a draw" << endl; 
        winner = 3; 
    }    
    else 
    { 
        cout<< "next player's turn"<< endl; 
    } 
    if (winner > 0) 
    { 
        done = true; 
    } 
return done; 
} 
 
int TicTacToe::Statistics(int winner)     
{ 
    playBall.y++; 
    switch(winner) 
    { 
    case 1: z++; 
        { 
        break; 
        } 
    case 2: t++; 
        { 
        break; 
        } 
    case 3: s++; 
        { 
        break; 
        } 
    } 
    cout<<" Number of games you have played: " << y; 
    cout<<" Number of games Player 1 has won: " << z << " games.";  
    cout<<" Number of games Player 2 has won: " << t << " games."; 
    cout<<" Number have draws you had: " << s << " draws."; 
    return 0; 
} 
 
char TicTacToe::playAgain(char done) 
{ 
    cout <<"Would you like to play again"<< endl; 
    cout <<"Y/N: " << endl; 
    cin >> done; 
    if(done == 'Y' || done == 'y') 
    { 
         done = false; 
    } 
    else 
    { 
        done = true; 
    } 
    return done; 
} 
void TicTacToe::boardProgress(char x[][3]) 

{
	cls.setForeground(Purple);

    cout << "      |     |     \n"; 
    cout << "   " << x[0][0] << "  |  " << x[0][1] << "  |  " << x[0][2] <<"  \n"; 
    cout << " _____|_____|_____\n"; 
    cout << "      |     |     \n"; 
    cout << "   " << x[1][0] << "  |  " << x[1][1] << "  |  " << x[1][2] <<"  \n"; 
    cout << " _____|_____|_____\n"; 
    cout << "      |     |     \n"; 
    cout << "   " << x[2][0] << "  |  " << x[2][1] << "  | " << x[2][2] <<"  \n"; 
    cout << "      |     |     \n"; 
} 


Was This Post Helpful? 0
  • +
  • -

#13 Sharan93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-April 12

Re: Tic Tac Toe using classes and algorithms, c++ help please

Posted 07 April 2012 - 06:59 AM

View PostSharan93, on 07 April 2012 - 05:54 AM, said:

View Postjimblumberg, on 06 April 2012 - 11:27 AM, said:

If you are talking about the "question" you asked in this topic: help with sorting out TIC TAC TOE Code the question is about the same topic. Post the code and ask the question here.

Jim

Okay thanks, my question is how can I change the board for the following code and have the board which is in the above code?
#include<iostream>
#include<string>
#include"110ct.h"
using namespace std;

class TicTacToe 
{ 
private: 
	ColourController ct;
    ColourController cls; 
    CursorController crs;
    char Board[3][3]; 
    int play(); 
    void playermove(); 
    int turn(int); 
    int check (int, int); 
    void boardProgress(char x[][3]); 
    int Winner(char x[][3]); 
    int player1, player2, draw, player,winner,done; 
    int row, col; 
 
public: 
    int y, z, t, s; 
    void intBoard(); 
    int Statistics(int); 
 
    char playAgain(char); 
}playBall; 
 
int main() 
{ 
    playBall.y = 0,playBall.z = 0,playBall.t = 0,playBall.s = 0; 
    int Winner = 0; 
	ColourController ct;
    ColourController cls; 
    cls.setForeground(Aqua);
        cout << " welcome to TIC TAC TOE" <<endl; 
 
    char done = false; 
 
    while (!done) 
        { 
        ColourController cls; 
        cls.setForeground(Aqua);
        cout << " Good Luck!"<< endl;
                 
 
        
        playBall.intBoard(); 
 
    
    done = playBall.playAgain(done); 
} 
 

cout << "Thank you for my playing my game"<< endl; 
system ("pause"); 
return 0; 
} 
void TicTacToe::intBoard() 
{ 
    done = false; 
    winner = 0; 
    player = 2; 
    player1 = 1; 
    player2 = 2; 
    draw = 3; 
    int i,j; 
    for (j=0;j<3;j++) 
    { 
        for (i=0;i<3;i++) 
        { 
            Board[j][i]=0; 
        } 
    } 
    play(); 
} 
 
int TicTacToe::play() 
{ 
    int done = false; 
    boardProgress(Board); 
    while(!done) 
    { 
        playermove(); 
        boardProgress(Board); 
        done = Winner(Board); 
    } 
    Statistics(winner); 
    return 0; 
} 
void TicTacToe::playermove() 
{ 
    int answer = false; 
    while (answer == false) 
    { 
        cout <<"Row: "; 
        cin >> row; 
        row--; 
        cout <<"Column: "; 
        cin >> col; 
        col--; 
        answer = check(row,col); 
    } 
    player = turn(player); 
    if (player == 1) 
        Board[row][col] = 'X'; 
    else if (player == 2) 
        Board[row][col] = 'O'; 
    else 
        cout<< "Failed."; 
} 
int TicTacToe::check(int row, int col) 
{ 
    if(Board[row][col] == 0) 
    { 
        return true; 
    } 
    else if (Board[row][col] != 0) 
    { 
        cout <<"You can't go there"<< endl; 
        return false; 
    } 
    else 
    { 
        cout<< "Going through Check"; 
        Winner(Board); 
    } 
    return false; 
} 
 
int TicTacToe::turn(int player) 
{ 
    switch(player) 
    { 
    case 1: player = 2; 
        { 
        cout<<"It's Player 1's turn"<< endl; 
        break; 
        } 
    case 2: player = 1; 
        { 
        cout<<"It's Player 2's turn"<< endl; 
        break; 
        } 
    } 
    return player; 
} 
 
int TicTacToe::Winner(char x[][3]) 
{ 
    winner = 0; 
    int count = 0; 
    int a = row; 
    int b = col; 
    for (a=0;a<3;a++) 
    { 
        for (b=0;b<3;b++) 
        { 
            if (Board[a][b]==0) 
            { 
                count++; 
            } 
        } 
    } 
    if (count > 0) 
    { 
        int row, col, r, c, d, r1, c1, d1; 
        for (row=0; row<3; row++) 
        { 
            r=0; 
            r1=0; 
 
            for (col=0; col<3; col++) 
            { 
			
                if(x[row][col]=='X') 
                    r++; 
                if(x[row][col]=='O') 
                    r1++; 
                if (r1==3) 
                {  
                    winner=2; 
                } 
                if (r==3) 
                {    
                    winner=1; 
                } 
            } 
        } 
        for (col=0; col<3; col++) 
        { 
            r=0; 
            r1=0; 
            for (row=0; row<3; row++) 
            { 
                    if(x[row][col]=='X') 
                    r++; 
                if(x[row][col]=='O') 
                    r1++; 
                if (r1==3) 
                {    
                    winner=2; 
                } 
                if (r==3) 
                { 
                    winner=1; 
                    } 
            } 
        } 
        if (x[0][0]=='X' && x[1][1]=='X' && x[2][2]=='X') 
        { 
            winner=1; 
        } 
        else if (x[0][0]=='O' && x[1][1]=='O' && x[2][2]=='O') 
        { 
            winner=2; 
        } 
        else if (x[2][0]=='X' && x[1][1]=='X' && x[0][2]=='X') 
        { 
            winner=1; 
        } 
        else if (x[2][0]=='O' && x[1][1]=='O' && x[0][2]=='O') 
        { 
            winner=2; 
        } 
    } 
    else if (count == 9) 
    { 
        cout << "Its a draw" << endl; 
        winner = 3; 
    }    
    else 
    { 
        cout<< "next player's turn"<< endl; 
    } 
    if (winner > 0) 
    { 
        done = true; 
    } 
return done; 
} 
 
int TicTacToe::Statistics(int winner)     
{ 
    playBall.y++; 
    switch(winner) 
    { 
    case 1: z++; 
        { 
        break; 
        } 
    case 2: t++; 
        { 
        break; 
        } 
    case 3: s++; 
        { 
        break; 
        } 
    } 
    cout<<" Number of games you have played: " << y; 
    cout<<" Number of games Player 1 has won: " << z << " games.";  
    cout<<" Number of games Player 2 has won: " << t << " games."; 
    cout<<" Number have draws you had: " << s << " draws."; 
    return 0; 
} 
 
char TicTacToe::playAgain(char done) 
{ 
    cout <<"Would you like to play again"<< endl; 
    cout <<"Y/N: " << endl; 
    cin >> done; 
    if(done == 'Y' || done == 'y') 
    { 
         done = false; 
    } 
    else 
    { 
        done = true; 
    } 
    return done; 
} 
void TicTacToe::boardProgress(char x[][3]) 

{
	cls.setForeground(Purple);

    cout << "      |     |     \n"; 
    cout << "   " << x[0][0] << "  |  " << x[0][1] << "  |  " << x[0][2] <<"  \n"; 
    cout << " _____|_____|_____\n"; 
    cout << "      |     |     \n"; 
    cout << "   " << x[1][0] << "  |  " << x[1][1] << "  |  " << x[1][2] <<"  \n"; 
    cout << " _____|_____|_____\n"; 
    cout << "      |     |     \n"; 
    cout << "   " << x[2][0] << "  |  " << x[2][1] << "  | " << x[2][2] <<"  \n"; 
    cout << "      |     |     \n"; 
} 


sorry realised posted the same code, I want the grid same as the above code for the following code, how can I chang ethat?
#include <string>
#include <iostream>
#include"110ct.h"
//Use standard namespace to eliminate typing of std::
using namespace std;

/* !-------- Declare Classes Here --------! */

class TicTacToe
{
   private:
      int Grid[9];
      int Player;
      int TurnCounter;
      int Players;
      int PlayerType[20];
   public:
      TicTacToe();
      void PrintTurn();
      int PlayerHuman();
      void HumanMove();
      void ComputerMove();
      void DrawBoard();
      int  Winner();
      int  FullBoard();
      void NextTurn();
}; //End of the Class Declaration

/* !-------- Develop Methods Here --------! */

TicTacToe::TicTacToe()
{
    srand(time(0)); //Get a Random Number
    Player = 1; //Determines who starts first
    TurnCounter = 0; //Initializes the turn counter
        
//Set up New Players when game begins
    Players = 2;
    PlayerType[1] = 1; //PlayerType[Player] is the Human User(1)
    PlayerType[2] = 0; //PlayerType[Player] is the Computer (0)

    for (int i = 0; i < 9; i++)
    {
       Grid[i] = 0;
    }
}

void TicTacToe::DrawBoard()
{
	ColourController cls;
	cls.setForeground(Red);
   cout << endl
        << Grid[0] << " | " << Grid[1] << " | " << Grid[2]
        << "\n---------\n"
        << Grid[3] << " | " << Grid[4] << " | " << Grid[5]
        << "\n---------\n"
        << Grid[6] << " | " << Grid[7] << " | " << Grid[8]
        << endl;
} //End Drawing the TicTacToe Board

void TicTacToe::PrintTurn()
{
   cout << "\nPlayer " << Player << "'s turn.\n";
} //End Printing Turn

void TicTacToe::NextTurn()
{
   TurnCounter++;

   if (++Player > Players)
   {
      Player = 1;
   }
} //End Next Turn

int TicTacToe::PlayerHuman()
{
   return PlayerType[Player];
} //End Determination of A Human Player

void TicTacToe::HumanMove()
{
	ColourController cls;
	cls.setBackground(Blue);
	cls.setForeground(Aqua);
   cout << " Enter your box number please: ";
   int Move;

   do
   {
      cin >> Move;
      Move--; // For user to enter 1-9 instead of 0-8
   }
   while (Move < 0 || Move > 8 || Grid[Move] != 0);
   Grid[Move] = Player;
} //End the Human Move

void TicTacToe::ComputerMove()
{
   int Move;

   do
   {
      Move  = rand() % 9; //Not really a Smart AI, Just picks a random move
   }
   while (Move < 0 || Move > 8 || Grid[Move] != 0);
   Grid[Move] = Player;
} //End the Computer Move

int TicTacToe::Winner()
{
   int i = 0;
   int Board[8][3] = {
	                
	   {0,1,2},
       {3,4,5},
       {6,7,8},
       {0,3,6},
       {1,4,7},
       {2,5,8},
       {0,4,8},
       {2,4,6}
   }; //List of all possible winning outcomes

   for ( i = 0; i < 8; i++) //Loop for all possible winning outcomes
   {
      if ((Grid[Board[i][0]] == Grid[Board[i][1]]) && (Grid[Board[i][1]] == Grid[Board[i][2]]) && Grid[Board[i][0]] != 0 )
      {
         cout << "Player " << Grid[Board[i][0]]
              <<  " IS THE WINNER!" <<endl;

         system("pause");
         return 1;  //Returns a true value (1 with the bool data type).
      }
   }
   return 0; //Else a winner has not been found yet, and return a false (0) value
} //End Checking the Winner

int TicTacToe::FullBoard()
{
   if (TurnCounter == 9)
   {
      cout << "DRAW" <<endl;
      return 1;
   }
   else
   {
      return 0;
   }
} //End Checking for a Full Board

int main(void)
{

   TicTacToe game;
   ColourController cls;
   cls.setForeground(Green);
   cout <<"Welcome! Below is the board to play this game, you must press the number on your keyboard to chose your box!" << endl;
   
    cout    << " 1 | 2 | 3 " << endl;
    cout    << "-----------" << endl;
    cout    << " 4 | 5 | 6 " << endl;
    cout    << "-----------" << endl;
    cout    << " 7 | 8 | 9 " << endl;
        
   game.DrawBoard();
   
   do
   {
      game.PrintTurn();
      if (game.PlayerHuman()) //If it is the Human turn, allow the human to move
      {
         game.HumanMove();
      }
      else //Else if it is not the Human turn, it is the computer's turn
      {
         game.ComputerMove();
      }
      
      game.DrawBoard();
      game.NextTurn();
    }
    while (!game.Winner() && !game.FullBoard()); //Execute the code in the do loop while there is no winner
                                                     //And the game board is not full
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1