Pointer Question..... C++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

Re: Pointer Question..... C++

Post by villeballa89 »

ahh right..... hmm I see that it draws everything but, I can't really read it. the whole this->pointer I haven't really covered yet. and I deffinately don't know what this does:

Code: Select all

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
still new to C++ so that may be why I'm not understanding that bit though. No problem, just if ya had someoone who knew a little more basic knowledge it would most likely work better for them.

btw is there something similar to what I was trying to do that the C++ will allow? (meaning the list of referances, or something like it)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post by avansc »

villeballa89 wrote:ahh right..... hmm I see that it draws everything but, I can't really read it. the whole this->pointer I haven't really covered yet. and I deffinately don't know what this does:

Code: Select all

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
still new to C++ so that may be why I'm not understanding that bit though. No problem, just if ya had someoone who knew a little more basic knowledge it would most likely work better for them.

btw is there something similar to what I was trying to do that the C++ will allow? (meaning the list of referances, or something like it)
C/C++ does not have any graphics stuff. so the function gotoxy with

Code: Select all

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
just makes it possible to gotoxy.

this->whatever, okay this just means that use "this" class, and go to the whatever varible. its used when you have function that have parameters that are the same names ase class variables. so then you could say this->var = var, the compiler will know that var is the value in the function parameter, and this->var is the varible of the class.

why do you want a list of references for tic tac toe. a list of references is actually quite an advanced C/C++ concept. what i gave you is about the bare basic version, the only realy somewhat advanced concept is that i pass a entire class as a variable. but yeah, its notthing major.

i have a question, do you have to have AI for your TTT? or is it a 2 player only thing?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

Re: Pointer Question..... C++

Post by villeballa89 »

why do you want a list of references for tic tac toe.
with the way I was writing it, it seemed like the most logical/ easiest way to go about doing it. Then again, that might just be the way I was thinking/ how my mind works. anyway, I went to using my original code (mainly cause I understood it) and tried an array of characters instead of an array of references. The only problem is that I can't get it to initialize right....... and I still don't understand what that piece of code does......


~edit~ and yes, there has to be a very basic AI (random placement would work)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post by avansc »

villeballa89 wrote:
why do you want a list of references for tic tac toe.
with the way I was writing it, it seemed like the most logical/ easiest way to go about doing it. Then again, that might just be the way I was thinking/ how my mind works. anyway, I went to using my original code (mainly cause I understood it) and tried an array of characters instead of an array of references. The only problem is that I can't get it to initialize right....... and I still don't understand what that piece of code does......


~edit~ and yes, there has to be a very basic AI (random placement would work)
that piece of code does this.
gotoxy(10,10)
cout << "hello";

hello will be printed out at 10,10 on the terminal screen.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

TicTacToe (finally finished)

Post by villeballa89 »

Final version of my tictactoe, for anyone interested. The AI is no good (since it is absent) but the fun comes in trying to make O win.

Code: Select all

#include <iostream>
#include <string>

using namespace std;

string spaces5 = "     ";//time saver
string& s5 = spaces5;

string underscore5 = "_____";// another time saver
string& u5 = underscore5;

char grid[] = ".........";// game piece slots
string w;
int i = 1;// standard loop variable
char playAgain;// user input to play again

class GameRules
{
    public:
        void xMove(void);
        void oMove(void);
        bool winner;
        bool turn;// true == o turn, false == x turn
        int tempMove;
        void win();
}r;
class Gameboard
{
        
        
    public:  
       
        void drawRow1(void);
        void drawRow2(void);
        void drawRow3(void);        
}t;
void GameRules::xMove()
{
    cout<<"\tX Please select a location to place your piece"<<endl<<"\t>";
    cin>> tempMove;
    while (turn == false)
    {
        
        if (tempMove > 0 && tempMove < 10 && grid[tempMove - 1] == '.')
        {
            grid[tempMove - 1] = 'X';
            turn = true;
        }
        else
        {
            cout<<endl<<"\tPlease enter a correct location"<<endl<<"\t>";
            cin>> tempMove;
            
        }
             
    }
}

void GameRules::oMove()
{
    if( grid[0] == '.' && winner == false || grid[1] == '.' && winner == false ||
        grid[2] == '.' && winner == false || grid[3] == '.' && winner == false ||
        grid[4] == '.' && winner == false || grid[5] == '.' && winner == false ||
        grid[6] == '.' && winner == false || grid[7] == '.' && winner == false ||
        grid[8] == '.' && winner == false)// prevents infinate loop and o from going after x wins
        {
    while (turn == true)
    {
        tempMove = rand() %9 + 1;
        if(grid[tempMove - 1] == '.')
        {
            grid[ tempMove - 1] = 'O';
            turn = false;
            system("pause");
        }
        else 
        {
           turn == true; 
        }
    }
    }
    
}
////////////////////////////////////////////Terms for winning/ Losing
void GameRules::win()
{
    if (grid[0] == grid[1] && grid[1] == grid[2] && grid[0] != '.')
    {
        w = grid[0];
        winner = true;
        
    }
    else if (grid[3] == grid[4] && grid[4] == grid[5]&& grid[3] != '.')
    {
        w = grid[3];
        winner = true;
        
    }
    else if (grid[6] == grid[7] && grid[7] == grid[8]&& grid[6] != '.')
    {
        w = grid[6];
        winner = true;
    }
    else if (grid[0] == grid[3] && grid[3] == grid[6]&& grid[0] != '.')
    {
        w = grid[0];
        winner = true;
    }
    else if (grid[1] == grid[4] && grid[4] == grid[7]&& grid[1] != '.')
    {
        w = grid[1];
        winner = true;
    }
    else if (grid[2] == grid[5] && grid[5] == grid[8]&& grid[2] != '.')
    {
        w = grid[2];
        winner = true;
    }
    else if (grid[0] == grid[4] && grid[4] == grid[8]&& grid[0] != '.')
    {
        w = grid[0];
        winner = true;
    }
    else if (grid[4] == grid[2] && grid[2] == grid[6]&& grid[4] != '.')
    {
        w = grid[4];
        winner = true;
    }
    else if ( grid[0] == '.' || grid[1] == '.' || grid[2] == '.' || grid[3] == '.'
        || grid[4] == '.' || grid[5] == '.' || grid[6] == '.' || grid[7] == '.'
        || grid[8] == '.')
    {
        winner = false;
    }
    else
    {
        w = "Cat";
        winner = true;
    }
}
///////////////////////////////////end Win

void Gameboard::drawRow1()// draws top row
{
    cout<<"\t"<<s5<<"1"<<s5<<"|"<<s5<<"2"<<s5<<"|"<<s5<<"3"<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<grid[0]<<s5<<"|"<<s5<<grid[1]<<s5<<"|"<<s5<<grid[2]<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<u5<<"_"<<u5<<"|"<<u5<<"_"<<u5<<"|"<<u5<<"_"<<u5<<endl;
}

void Gameboard::drawRow2()// draws middle row
{
    cout<<"\t"<<s5<<"4"<<s5<<"|"<<s5<<"5"<<s5<<"|"<<s5<<"6"<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<grid[3]<<s5<<"|"<<s5<<grid[4]<<s5<<"|"<<s5<<grid[5]<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<u5<<"_"<<u5<<"|"<<u5<<"_"<<u5<<"|"<<u5<<"_"<<u5<<endl;
}

void Gameboard::drawRow3()// draws bottom row
{
    cout<<"\t"<<s5<<"7"<<s5<<"|"<<s5<<"8"<<s5<<"|"<<s5<<"9"<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<grid[6]<<s5<<"|"<<s5<<grid[7]<<s5<<"|"<<s5<<grid[8]<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl
        <<"\t"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<"|"<<s5<<" "<<s5<<endl;
}


int main()
{
	while (i > 0)
	{
    system("cls");
	r.turn = rand() % 1;
	for (i = 0; i < 9; i++)
	{
        grid[i] = '.';
    }
    r.winner = false;
    
	while(r.winner == false)
    {
		
        t.drawRow1();
		t.drawRow2();
		t.drawRow3();
        r.xMove();
        r.win();
		system("cls");
        t.drawRow1();
		t.drawRow2();
		t.drawRow3();
        
        r.oMove();
        r.win();
        system("cls");
		t.drawRow1();
		t.drawRow2();
		t.drawRow3();
        system("pause");
        system("cls");
		
		
    }
    cout<<"\t"<<w<<"wins!"<<endl;
    cout<<"\tWould you like to play again? y/n"<<endl<<"\t>";
    cin>> playAgain;
    if (playAgain == 'y')
    {
        i = 1;
    }
    else if (playAgain == 'Y')
    {
        i = 1;
    }
    else
    {
        i = 0;
    }
    }
    
    return 0;
}

also, if anyone would mind critiquing it (style, OOP practices) I would greatly appreciate it. Even if you say " this is the worst build of such an easy game I've ever seen" it would help, at least I know where I'm at :p. But yea, like I said, try to make O win, it's harder than it sounds.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

TTT critiquing.

Post by avansc »

all what i am about to say is meant to be constructive. and i also want to say good job. conception OO is easy, doing it right is not.

1. in a proparly OO designed program you will have few or preferably no global variables. you only use global variables.

2 these are not good classes, here are the reasons.
  • 1. They dont encapsulate the objects properly. for example gameboard only has draw functions. it should have a member variable that hold the state of the board. you have grid as global, that should be in your class. the idea of classes are that you can write them, and then use them anywhere. your program will only work as it is with the global variables, which is bad.
    2. why do you have the r and the t at the end of your classes??? i know you saw it in a book, but you misunderstood why thats done. its used in conjunction with typedef and is really meant to be used with structures.
    3. GameRules and GameBoard should be able to "talk" to each other. ie. games rules shouyld be able to modify a gameboard variable. ie. pass the gameboard variable as a reference.
3. lastly you should put each class declaration in its own h file like GameRules.h and then the implimentation of that class in GameRules.cpp, then in main.cpp just have #include <"GameRules.h">

Code: Select all

class GameRules
{
    public:
        void xMove(void);
        void oMove(void);
        bool winner;
        bool turn;// true == o turn, false == x turn
        int tempMove;
        void win();
}r;
class Gameboard
{
       
       
    public: 
       
        void drawRow1(void);
        void drawRow2(void);
        void drawRow3(void);       
}t;
as it is your program really is not OO. if you would like i will write a clone of your program using proper OO.
i know you are just starting, so dont get discourage. you should have seen my code when i started, was much worse than this. you just need to keep at it.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

Re: Pointer Question..... C++

Post by villeballa89 »

if you don't mind, and have the time, I'd actually enjoy looking at a clone of mine being properly written. And like I said, I know it was nowhere near efficient so thanks for the tips.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post by avansc »

villeballa89 wrote:if you don't mind, and have the time, I'd actually enjoy looking at a clone of mine being properly written. And like I said, I know it was nowhere near efficient so thanks for the tips.

okay, i'll post it later tonight. i just got home and have some stuff to do.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post by avansc »

villeballa89 wrote:if you don't mind, and have the time, I'd actually enjoy looking at a clone of mine being properly written. And like I said, I know it was nowhere near efficient so thanks for the tips.

here is a basic OO solution. i didnt complete the program, but you can see the OO concept.
if you struggle with the AI, please let me know and i'll make an AI for you that cant lose.

main.cpp

Code: Select all

#include <iostream>
#include <string>
#include <windows.h>

#include "GameBoard.h"
#include "GameRules.h"

using namespace std;

int main()
{
	GameBoard *gb = new GameBoard();
	GameRules *gr = new GameRules();

	gr->xMove(gb, 1, 1);
	gr->xMove(gb, 2, 1);
	gr->xMove(gb, 3, 1);
	gb->draw();

	cout << endl;
	if(gr->checkWin(gb))
	{
		cout << "WIN";
	}
	cout << endl;
	system("pause");
    return 0;
}

GameRules.h

Code: Select all

#ifndef _gamerules_h
#define _gamerules_h

#include "GameBoard.h"	// We have to include the GameBoard.h file, because we use GameBoard object

class GameRules
{
public:
	GameRules();
	bool xMove(GameBoard *gb, int x, int y);
    bool oMove(GameBoard *gb, int x, int y);
	bool checkWin(GameBoard *gb);
	char currentMove;
private:
};

#endif
GameBoard.h

Code: Select all

#ifndef _gameboard_h
#define _gameboard_h

class GameBoard
{
public:
	GameBoard();
	void draw();
	void gotoxy(int x, int y);
	char grid[3][3];
private:
};

#endif
GameRules.cpp

Code: Select all

#include <iostream>
#include <string>
#include <windows.h>
#include "GameRules.h"
#include "GameBoard.h"

using namespace std;

GameRules::GameRules()
{
	this->currentMove = 'X';
}

bool GameRules::checkWin(GameBoard *gb)
{
	for(int y = 0;y < 3;y++)
	{
		if((gb->grid[y][0]+gb->grid[y][1]+gb->grid[y][2])/this->currentMove == 3)
			return true;
	}

	for(int x = 0;x < x;x++)
	{
		if((gb->grid[0][x]+gb->grid[1][x]+gb->grid[2][x])/this->currentMove == 3)
			return true;
	}

	if(gb->grid[0][0] == gb->grid[1][1] == gb->grid[2][2] == this->currentMove)
			return true;

	if(gb->grid[2][0] == gb->grid[1][1] == gb->grid[0][2] == this->currentMove)
			return true;
	return false;
}

bool GameRules::xMove(GameBoard *gb, int x, int y)
{
	if(x > 3 || y > 3)
		return false;

	if(gb->grid[y-1][x-1] != ' ')
		return false;

	gb->grid[y-1][x-1] = 'X';

	return true;
}
GameBoard.cpp

Code: Select all

#include <iostream>
#include <string>
#include <windows.h>
#include "GameBoard.h"

using namespace std;

GameBoard::GameBoard()
{
	for(int y = 0;y < 3;y++)
	{
		for(int x = 0;x < 3;x++)
		{
			this->grid[y][x] = ' ';
		}
	}
}

void GameBoard::draw()
{
	system("cls");
	cout << "-------" << endl;
	cout << "| | | |" << endl;
	cout << "-------" << endl;
	cout << "| | | |" << endl;
	cout << "-------" << endl;
	cout << "| | | |" << endl;
	cout << "-------" << endl;

	for(int y = 0;y < 3;y++)
	{
		for(int x = 0;x < 3;x++)
		{
			gotoxy(x*2+1,y*2+1);
			cout << this->grid[y][x] << endl;
		}
	}
}

void GameBoard::gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

to conclude, if i was going to complete this program i would add a class called GameWorld, that handeled everything for me, so that i would just have a main game loop in the main.cpp. I would also add a AI class that could take a GameBoard and GameRules objects as parameters. this would then be a completely OO solution to the problem. i hope i helped at least a bit. please feel free if you have any questions.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply