Hi all, just completed my first c++ program! After asking some questions on these and other boards, i was able to complete it.
lol, its definitely not the cleanest code, and i was just programming as i went, adding more code to fix problems - which isn't the best way. Let me know your suggestions, and i will work on to improve in my next project.Code:#include <iostream> #include <stdlib.h> #include <string> using namespace std; int playerturn; string boardarray[4][4]; bool boardarray2[4][4]; bool winstatus; struct drawmenu(){ cout << "O----------------------O " << endl; cout << "| TIC-TAC-TOE | " << endl; cout << "| by: Andy Hin | " << endl; cout << "| | " << endl; cout << "| [1] Begin Game | " << endl; cout << "| | " << endl; cout << "| [2] Quit Game | " << endl; cout << "| | " << endl; cout << "O----------------------O " << endl; } struct drawboard(){ cout << " 1 2 3 " << endl; cout << " " << endl; cout << "1 "<<boardarray[1][1]<<" | "<<boardarray[1][2]<<" | "<<boardarray[1][3]<< endl; cout << " | | " << endl; cout << " -----------------" << endl; cout << "2 "<<boardarray[2][1]<<" | "<<boardarray[2][2]<<" | "<<boardarray[2][3]<< endl; cout << " | | " << endl; cout << " -----------------" << endl; cout << "3 "<<boardarray[3][1]<<" | "<<boardarray[3][2]<<" | "<<boardarray[3][3]<< endl; cout << " | | " << endl; } struct winchecko(){ if (boardarray[1][1]=="O" && boardarray[1][2]=="O" && boardarray[1][3]=="O"){ cout << "O wins, top row!" << endl; winstatus=true; }else if (boardarray[2][1]=="O" && boardarray[2][2]=="O" && boardarray[2][3]=="O"){ cout << "O wins, middle row!" << endl; winstatus=true; }else if (boardarray[3][1]=="O" && boardarray[3][2]=="O" && boardarray[3][3]=="O"){ cout << "O wins, bottom row!" << endl; winstatus=true; }else if (boardarray[1][1]=="O" && boardarray[2][1]=="O" && boardarray[3][1]=="O"){ cout << "O wins, first collumn!" << endl; winstatus=true; }else if (boardarray[1][2]=="O" && boardarray[2][2]=="O" && boardarray[3][2]=="O"){ cout << "O wins, second collumn!" << endl; winstatus=true; }else if (boardarray[1][3]=="O" && boardarray[2][3]=="O" && boardarray[3][3]=="O"){ cout << "O wins, third collumn!" << endl; winstatus=true; }else if (boardarray[1][1]=="O" && boardarray[2][2]=="O" && boardarray[3][3]=="O"){ cout << "O wins, diagonally!" << endl; winstatus=true; }else if (boardarray[3][1]=="O" && boardarray[2][2]=="O" && boardarray[1][3]=="O"){ cout << "O wins, diagonally!" << endl; winstatus=true; } } struct wincheckx(){ if (boardarray[1][1]=="X" && boardarray[1][2]=="X" && boardarray[1][3]=="X"){ cout << "X wins, top row!" << endl; winstatus=true; }else if (boardarray[2][1]=="X" && boardarray[2][2]=="X" && boardarray[2][3]=="X"){ cout << "X wins, middle row!" << endl; winstatus=true; }else if (boardarray[3][1]=="X" && boardarray[3][2]=="X" && boardarray[3][3]=="X"){ cout << "X wins, bottom row!" << endl; winstatus=true; }else if (boardarray[1][1]=="X" && boardarray[2][1]=="X" && boardarray[3][1]=="X"){ cout << "X wins, first collumn!" << endl; winstatus=true; }else if (boardarray[1][2]=="X" && boardarray[2][2]=="X" && boardarray[3][2]=="X"){ cout << "X wins, second collumn!" << endl; winstatus=true; }else if (boardarray[1][3]=="X" && boardarray[2][3]=="X" && boardarray[3][3]=="X"){ cout << "X wins, third collumn!" << endl; winstatus=true; }else if (boardarray[1][1]=="X" && boardarray[2][2]=="X" && boardarray[3][3]=="X"){ cout << "X wins, diagonally!" << endl; winstatus=true; }else if (boardarray[3][1]=="X" && boardarray[2][2]=="X" && boardarray[1][3]=="X"){ cout << "X wins, diagonally!" << endl; winstatus=true; } } struct drawcheck(){ if (playerturn==9 && winstatus==false){ cout << "Cats Game!" << endl; } } int main(){ boardarray[1][1]=" "; boardarray[1][2]=" "; boardarray[1][3]=" "; boardarray[2][1]=" "; boardarray[2][2]=" "; boardarray[2][3]=" "; boardarray[3][1]=" "; boardarray[3][2]=" "; boardarray[3][3]=" "; int menuselect=0; int moverow; int movecollumn; drawmenu(); cin >> menuselect; if (menuselect=1){ cout << "Being Game..." << endl; drawboard(); } do{ cout << " Please Make a move"<<endl; cout << " Row:"; cin >> moverow; cout << " Collumn: "; cin >> movecollumn; if (playerturn%2==1 && boardarray2[moverow][movecollumn]==false){ boardarray[moverow][movecollumn]= "X"; boardarray2[moverow][movecollumn]=true; playerturn++; } else if (playerturn%2==0 && boardarray2[moverow][movecollumn]==false){ boardarray[moverow][movecollumn]= "O"; boardarray2[moverow][movecollumn]=true; playerturn++; } wincheckx(); winchecko(); drawcheck(); drawboard(); }while (winstatus==false); return 0; }



LinkBack URL
About LinkBacks


