ssjninja Posted May 20, 2007 Share Posted May 20, 2007 hey guys i have been having some problems with this tic tac toe program im working on could u guys help me out the problem is taht player x wins randomly //Programed by viro //Email:***** //Release Date: //Description: #include <iostream.h> #include <conio.h>//used for clrscr() #include <iomanip.h> //used for setw() and other stuff like it #include <math.h> //used for all upper math functions #include <ctype.h> //used for conversion or mesurement of an char or string #include <fstream.h>// used for writing and opening data files #include <string.h> #include "matrix.h" //gobal variables // //prototypes // main() { char board[3][3] ; int row=0; int col=0; int z=0; int winner=0; char choice_x; char choice_o; clrscr(); board[0][0]=97; board[0][1]=98; board[0][2]=99; board[1][0] =100; board[1][1]=101; board[1][2] =102; board[2][0]=103; board[2][1]=104 ; board[2][2]=105; do { cout<< " Welcome To TicTacToe " << endl << "please enter the letter that is in the spot you would like to move" << " " << " ³" << " " << "³" << " " << endl << " "<< board[0][0] <<" " << "³" << " "<< board[0][1] <<" " << "³" << " "<< board[0][2] <<" " << endl << " ÄÄÄ" << "Å" << "ÄÄÄ" << "Å" << "ÄÄÄ" << endl << " " << "³" << " " << "³" << " " << endl; cout << " "<< board[1][0] <<" " << "³" << " "<< board[1][1] <<" " << "³" << " "<< board[1][2] <<" " << endl << " ÄÄÄ" << "Å" << "ÄÄÄ" << "Å" << "ÄÄÄ" << endl << " " << "³" << " " << "³" << " " << endl << " "<< board[2][0] <<" " << "³" << " "<< board[2][1] <<" " << "³" << " "<< board[2][2] <<" " << endl << "enter letter x : " ; cin >> choice_x; switch(choice_x) { case 'a': if (board[0][0]==97) { board[0][0]=88; z++; } break; case 'b': if (board[0][1]==98) { board[0][1]=88; z++; } break; case 'c': if (board[0][2]==99) { board[0][2]=88; z++; } break; case 'd': if (board[1][0]==100) { board[1][0]=88; z++; } break; case 'e': if (board[1][1]==101) { board[1][1]=88; z++; } break; case 'f': if (board[1][2]==102) { board[1][2]=88; z++; } break; case 'g': if (board[2][0]==103) { board[2][0]=88; z++; } break; case 'h': if (board[2][1]==104) { board[2][1]=88; z++; } break; case 'i': if (board[2][2]==88) { board[2][2]=88; z++; } break; default: cout <<"what part of letter do u not understand"; break; } cout<< " Welcome To TicTacToe " << endl << "please enter the letter that is in the spot you would like to move" << " " << " ³" << " " << "³" << " " << endl << " "<< board[0][0] <<" " << "³" << " "<< board[0][1] <<" " << "³" << " "<< board[0][2] <<" " << endl << " ÄÄÄ" << "Å" << "ÄÄÄ" << "Å" << "ÄÄÄ" << endl << " " << "³" << " " << "³" << " " << endl; cout << " "<< board[1][0] <<" " << "³" << " "<< board[1][1] <<" " << "³" << " "<< board[1][2] <<" " << endl << " ÄÄÄ" << "Å" << "ÄÄÄ" << "Å" << "ÄÄÄ" << endl << " " << "³" << " " << "³" << " " << endl << " "<< board[2][0] <<" " << "³" << " "<< board[2][1] <<" " << "³" << " "<< board[2][2] <<" " << endl << "enter letter O : " ; cin >> choice_o; switch(choice_o) { case ''a': if (board[0][0]==97) { board[0][0]=79; z++; } break; case ''b': if (board[0][1]==98) { board[0][1]=79; z++; } break; case 'c': if (board[0][2]==99) { board[0][2]=79; z++; } break; case 'd': if (board[1][0]==100) { board[1][0]=79; z++; } break; case ''e': if (board[1][1]==101) { board[1][1]=79; z++; } break; case 'f': if (board[1][2]==102) { board[1][2]=79; z++; } break; case 'g': if (board[2][0]==103) { board[2][0]=79; z++; } break; case 'h': if (board[2][1]==104) { board[2][1]=79; z++; } break; case 'i': if (board[2][2]==105) { board[2][2]=79; z++; } break; default: cout <<endl <<"what part of letter do u not understand"; break; } if(z==9) {cout <<endl<< "OMGS CATS!lawl!noob";} if(board[0][0]==79&&board[0][1]==79&&board[0][2]||board[0][0]==79&&board[1][0]==79&&board[2][0]==79||board[0][0]==79&&board[1][1]==79&&board[2][2]==79||board[0][1]==79&&board[1][1]==79&&board[2][1]==79||board[0][2]==79&&board[1][1]==79&&board[2][0]==79||board[0][2]==79&&board[1][2]==79&&board[2][2]==79||board[1][0]==79&&board[1][1]==79&&board[1][2]==79||board[2][0]==79&&board[2][1]==79&&board[2][2]==79) { cout << " player O wins"; winner++ ; } if(board[0][0]==88&&board[0][1]==88&&board[0][2]||board[0][0]==88&&board[1][0]==88&&board[2][0]==88||board[0][0]==88&&board[1][1]==88&&board[2][2]==88||board[0][1]==88&&board[1][1]==88&&board[2][1]==88||board[0][2]==88&&board[1][1]==88&&board[2][0]==88||board[0][2]==88&&board[1][2]==88&&board[2][2]==88||board[1][0]==88&&board[1][1]==88&&board[1][2]==88||board[2][0]==88&&board[2][1]==88&&board[2][2]==88) { cout << " player X wins"; winner++ ; } }while(z>9||winner==0); getch(); return 0; } // ³=alt179 Å=alt197 Ä=alt196 Quote Link to comment Share on other sites More sharing options...
Shaun Posted May 20, 2007 Share Posted May 20, 2007 You know it would help if you said what the problem actually was. And posted some readable code. Your main problem is it's poorly designed and written. Quote Link to comment Share on other sites More sharing options...
ssjninja Posted May 20, 2007 Author Share Posted May 20, 2007 well duh its not pro code i have only been programing for about 2 months or so Quote Link to comment Share on other sites More sharing options...
cooper Posted May 21, 2007 Share Posted May 21, 2007 Okay, let's see now: #include <iomanip.h> //used for setw() and other stuff like it which you're not using #include <math.h> //used for all upper math functions which you're not using #include <ctype.h> //used for conversion or mesurement of an char or string which you're not doing #include <fstream.h>// used for writing and opening data files which you're not doing #include <string.h> why is this here? #include "matrix.h" What does this bring you? What's so different between what X and O do that you duplicate the functionality? Here's a few snippets to get you going... Initializing your board: #define BOARD_WIDTH 3 // It's a square, right? #define BOARD_HEIGHT BOARD_WIDTH for (height=0; height<BOARD_HEIGHT; height++) { for (width=0;width<BOARD_WIDTH; width++) { board[height][width] = 'a' + BOARD_WIDTH*height + width; } } Displaying the field: clrscr(); for (height=0; height<BOARD_HEIGHT; height++) { cout << "+---+---+---+" << endl; for (width=0;width<BOARD_WIDTH; width++) { cout << "| " << board[height][width] << " "; } cout << "|" << endl; } cout << "+---+---+---+" << endl; Filling out a position on the board given a user's input: #define ERROR -1; #define OK 1; height = (choice - 97) / 3; width = (choice - 97) % 3; if ((width < 0) || (width >= BOARD_WIDTH) || (height < 0) || (height >= BOARD_HEIGHT) || (board[height][width] != choice)) { cout << "input error"; return ERROR; } board[height][width] = player; return OK; Did anyone win: #define NO_WINNER ' ' //horizontals for (height = 0; height < BOARD_HEIGHT; height++) { for (width = 1; width < BOARD_WIDTH; width++) { if (board[height][width-1] != board[height][width]) break; } if (board[height][width-1] == board[height][width]) { return board[height][width]; } } //verticals for (width = 0; width < BOARD_WIDTH; width++) { for (height = 1; height < BOARD_HEIGHT; height++) { if (board[height-1][width] != board[height][width]) break; } if (board[height-1][width] == board[height][width]) { return board[height][width]; } } // diagonal - top left to bottom right for (position = 1; position < BOARD_HEIGHT; position++) { if (board[positioin][position] != board[positioin-1][position-1]) break; } if (board[positioin][position] == board[positioin-1][position-1]) { return board[positioin][position]; } // diagonal - top right to bottom left for (position = 1; position < BOARD_HEIGHT; position++) { if (board[positioin][bOARD_WIDTH-1 -position] != board[positioin-1][bOARD_WIDTH-position]) break; } if (board[positioin][bOARD_WIDTH-1 -position] == board[positioin-1][bOARD_WIDTH-position]) { return board[positioin][bOARD_WIDTH-1 -position]; } return NO_WINNER; Now, try again. And remember that it's okay to use more than one function for your program. In fact, it's a WHOLE lot better to do so. Quote Link to comment Share on other sites More sharing options...
ssjninja Posted May 21, 2007 Author Share Posted May 21, 2007 thanks the reason those includes are there its because our C++ teacher requires us to put all the includes we know how to use in a progam yea i know stupid Quote Link to comment Share on other sites More sharing options...
Shaun Posted May 21, 2007 Share Posted May 21, 2007 thanks the reason those includes are there its because our C++ teacher requires us to put all the includes we know how to use in a progam yea i know stupid I think you need to find a new C++ teacher. Quote Link to comment Share on other sites More sharing options...
ssjninja Posted May 21, 2007 Author Share Posted May 21, 2007 i personaly think that my school shouldn't use turbo c++ because it sucks and it makes me barely ablle to read ur code but i know its most likely written better lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.