My first sucessfull c++ game!
Posted: Thu Mar 04, 2010 8:26 pm
I can't help but to post this . This is the first actual game i have ever made, and its entirely MY code ! I made a TicTacToe clone. I used the code from my previous post, mvGLIB.h from the multidimensional array post, to create the game grid and i made some functions in another header file that have to do with win checking and stuff like that.
Here is main.cpp:
mvGLIB.h:
ttt.h (tictactoe):
What do you guys think of my code?
Any suggestions?
So, what game should i try to re-create next?
EDIT:
Oops, posted too early, forgot to write code for what happens when there is a tie
EDIT:
Ok, the last function on the ttt.h file with the EXTREMELY long if statement checks for ties. That if statement makes me hate parenthesis!
Here is main.cpp:
Code: Select all
#include "ttt.h" //includes mvGLIB.h and all other libs
int main(){
set_title("Tic Tac Toe");
cout<<"This is the first ever real game created by MV2112"<<endl;
cout<<"All source Copyrighted MV2112 2010"<<endl<<endl;
pause();
cout<<endl<<"Naaaaa, just kidding, its open source :)"<<endl<<endl;
pause();
while(1==1){
screen bord; //initialize screen
bord.setup(5,5); //setup grid
screen* pbord; //create pointer for editing grid
pbord=&bord; //link pointer to grid
bord.set_color(1);
char test; //return value for win function
char input; //used for game input
bool wincon=FALSE; //win condition boolean
bool c; //return value for check function
bool tcheck=FALSE; //TIE CHECK
int chc; //return value for insert function
char ch; //character char
char play; //play again var
//setting up chars for game table
bord.point(0,0,'1');
bord.point(1,0,'|');
bord.point(2,0,'2');
bord.point(3,0,'|');
bord.point(4,0,'3');
bord.point(0,1,'-');
bord.point(1,1,'|');
bord.point(2,1,'-');
bord.point(3,1,'|');
bord.point(4,1,'-');
bord.point(0,2,'4');
bord.point(1,2,'|');
bord.point(2,2,'5');
bord.point(3,2,'|');
bord.point(4,2,'6');
bord.point(0,3,'-');
bord.point(1,3,'|');
bord.point(2,3,'-');
bord.point(3,3,'|');
bord.point(4,3,'-');
bord.point(0,4,'7');
bord.point(1,4,'|');
bord.point(2,4,'8');
bord.point(3,4,'|');
bord.point(4,4,'9');
ch='X'; //player X goes first
//game loop starts here
while(wincon==FALSE){
tcheck=tiecheck(pbord);
if(tcheck==TRUE){
ch='t';
break;
}
bord.disp();
cout<<endl<<"Player "<<ch<<": ";
cin>>input;
switch(input){
case '1':
c=check(0,0,pbord);
chc=insert(c,0,0,pbord,ch);
if(chc==1){
continue;
}
break;
case '2':
c=check(2,0,pbord);
chc=insert(c,2,0,pbord,ch);
if(chc==1){
continue;
}
break;
case '3':
c=check(4,0,pbord);
chc=insert(c,4,0,pbord,ch);
if(chc==1){
continue;
}
break;
case '4':
c=check(0,2,pbord);
chc=insert(c,0,2,pbord,ch);
if(chc==1){
continue;
}
break;
case '5':
c=check(2,2,pbord);
chc=insert(c,2,2,pbord,ch);
if(chc==1){
continue;
}
break;
case '6':
c=check(4,2,pbord);
chc=insert(c,4,2,pbord,ch);
if(chc==1){
continue;
}
break;
case '7':
c=check(0,4,pbord);
chc=insert(c,0,4,pbord,ch);
if(chc==1){
continue;
}
break;
case '8':
c=check(2,4,pbord);
chc=insert(c,2,4,pbord,ch);
if(chc==1){
continue;
}
break;
case '9':
c=check(4,4,pbord);
chc=insert(c,4,4,pbord,ch);
if(chc==1){
continue;
}
break;
}
test=win(pbord);
if(test=='X'||test=='O'){
wincon=TRUE;
break;
}
if(ch=='X'){
ch='O';
}
else{
ch='X';
}
//game loop ends here
}
bord.disp();
if(ch=='t'){
cout<<"Nobody wins, it was a tie..."<<endl;
cout<<endl<<"Play again? Y or N"<<endl;
cin>>play;
strlwr(&play);
if(play=='y'){
continue;
}
if(play=='n'){
return 0;
}
}
cout<<endl<<"Congratulations!"<<endl<<"Player "<<ch<<" Won!!!"<<endl;
cout<<endl<<"Play again? Y or N"<<endl;
cin>>play;
strlwr(&play);
if(play=='y'){
continue;
}
if(play=='n'){
return 0;
}
}
}
Code: Select all
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <time.h>
#include <windows.h>
#include <conio.h>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
void MVcls(){
system("cls");
}
class object{
//object base class
public:
void set_char(char character);
char r_char();
vector<vector<char>> grid; // grid
vector<vector<char*>> pgrid; // pointer to grid
void set_frate(int t);
void frate();
void set_color(int num);
protected:
int x;
int y;
int ox;
int oy;
char ch;
int time;
};
void object::set_char(char character){
ch=character;
}
char object::r_char(){
return ch;
}
void object::set_frate(int t){
time=t;
}
void object::frate(){
Sleep(time);
}
void object::set_color(int num){
switch(num){
case 1:
system("color a");
break;
case 2:
system("color b");
break;
case 3:
system("color c");
break;
}
}
class screen:public object{
public:
void setup(int,int);
void rend(vector<vector<char*>>pgrid);
void disp();
void point(int x, int y, char a);
int r_x();
int r_y();
private:
int xx;
int yy;
};
void screen::rend(vector<vector<char*>>pgrid){
*pgrid[oy][ox]=' '; //this function is useless right now
*pgrid[y][x]=ch;
}
void screen::setup(int x, int y){
xx=x;
yy=y;
grid.resize(y);
pgrid.resize(y);
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
grid[j].resize(x);
grid[j][i]=ch;
pgrid[j].resize(x);
pgrid[j][i]=&grid[j][i];
}
}
}
void screen::disp(){
MVcls();
for(int i=0;i<yy;i++){
for(int j=0;j<xx;j++){
cout<<grid[i][j];
}
cout<<endl;
}
}
void screen::point(int x, int y, char a){
*pgrid[y][x]=a;
}
int screen::r_x(){
return xx;
}
int screen::r_y(){
return yy;
}
void set_title(string title){
string t;
t="title "+title;
system(t.c_str());
}
void pause(){
system("pause");
}
Code: Select all
#include "mvGLIB.h"
char win(screen* pbord){
if((*pbord).grid[0][0]=='X'&&(*pbord).grid[2][0]=='X'&&(*pbord).grid[4][0]=='X'){
return 'X';
}
if((*pbord).grid[0][2]=='X'&&(*pbord).grid[2][2]=='X'&&(*pbord).grid[4][2]=='X'){
return 'X';
}
if((*pbord).grid[0][4]=='X'&&(*pbord).grid[2][4]=='X'&&(*pbord).grid[4][4]=='X'){
return 'X';
}
if((*pbord).grid[0][0]=='X'&&(*pbord).grid[0][2]=='X'&&(*pbord).grid[0][4]=='X'){
return 'X';
}
if((*pbord).grid[2][0]=='X'&&(*pbord).grid[2][2]=='X'&&(*pbord).grid[2][4]=='X'){
return 'X';
}
if((*pbord).grid[4][0]=='X'&&(*pbord).grid[4][2]=='X'&&(*pbord).grid[4][4]=='X'){
return 'X';
}
if((*pbord).grid[0][0]=='X'&&(*pbord).grid[2][2]=='X'&&(*pbord).grid[4][4]=='X'){
return 'X';
}
if((*pbord).grid[0][4]=='X'&&(*pbord).grid[2][2]=='X'&&(*pbord).grid[4][0]=='X'){
return 'X';
}
// O
if((*pbord).grid[0][0]=='O'&&(*pbord).grid[2][0]=='O'&&(*pbord).grid[4][0]=='O'){
return 'O';
}
if((*pbord).grid[0][2]=='O'&&(*pbord).grid[2][2]=='O'&&(*pbord).grid[4][2]=='O'){
return 'O';
}
if((*pbord).grid[0][4]=='O'&&(*pbord).grid[2][4]=='O'&&(*pbord).grid[4][4]=='O'){
return 'O';
}
if((*pbord).grid[0][0]=='O'&&(*pbord).grid[0][2]=='O'&&(*pbord).grid[0][4]=='O'){
return 'O';
}
if((*pbord).grid[2][0]=='O'&&(*pbord).grid[2][2]=='O'&&(*pbord).grid[2][4]=='O'){
return 'O';
}
if((*pbord).grid[4][0]=='O'&&(*pbord).grid[4][2]=='O'&&(*pbord).grid[4][4]=='O'){
return 'O';
}
if((*pbord).grid[0][0]=='O'&&(*pbord).grid[2][2]=='O'&&(*pbord).grid[4][4]=='O'){
return 'O';
}
if((*pbord).grid[0][4]=='O'&&(*pbord).grid[2][2]=='O'&&(*pbord).grid[4][0]=='O'){
return 'O';
}
else{
return '0';
}
}
bool check(int x, int y,screen * pbord){
if((*pbord).grid[y][x]=='X'||(*pbord).grid[y][x]=='O'){
return FALSE;
}
else{
return TRUE;
}
}
int insert(bool c,int x, int y, screen * pbord, char ch){
if(c==TRUE){
(*pbord).grid[y][x]=ch;
return 0;
}
else{
return 1;
}
}
bool tiecheck(screen * pbord){
if(((*pbord).grid[0][0]=='X'||(*pbord).grid[0][0]=='O')&&((*pbord).grid[0][2]=='X'||(*pbord).grid[0][2]=='O')&&((*pbord).grid[0][4]=='X'||(*pbord).grid[0][4]=='O')&&((*pbord).grid[0][0]=='X'||(*pbord).grid[0][0]=='O')&&((*pbord).grid[0][2]=='X'||(*pbord).grid[0][2]=='O')&&((*pbord).grid[0][4]=='X'||(*pbord).grid[0][4]=='O')&&((*pbord).grid[2][0]=='X'||(*pbord).grid[2][0]=='O'&&(*pbord).grid[2][2]=='X'||(*pbord).grid[2][2]=='O')&&((*pbord).grid[2][4]=='X'||(*pbord).grid[2][4]=='O')&&((*pbord).grid[4][0]=='X'||(*pbord).grid[4][0]=='O')&&((*pbord).grid[4][2]=='X'||(*pbord).grid[4][2]=='O')&&((*pbord).grid[4][4]=='X'||(*pbord).grid[4][4]=='O')){
return TRUE;
}
else{
return FALSE;
}
}
Any suggestions?
So, what game should i try to re-create next?
EDIT:
Oops, posted too early, forgot to write code for what happens when there is a tie
EDIT:
Ok, the last function on the ttt.h file with the EXTREMELY long if statement checks for ties. That if statement makes me hate parenthesis!