Code: Select all
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
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)
Moderator: Coders of Rage
Code: Select all
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
C/C++ does not have any graphics stuff. so the function gotoxy withvilleballa89 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:
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.Code: Select all
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }
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)
Code: Select all
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
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......why do you want a list of references for tic tac toe.
that piece of code does this.villeballa89 wrote: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......why do you want a list of references for tic tac toe.
~edit~ and yes, there has to be a very basic AI (random placement would work)
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;
}
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;
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.
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.
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;
}
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
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
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;
}
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);
}