Need help w/logical errors in array

This is a discussion on Need help w/logical errors in array within the C++ Programming forums, part of the General Programming Boards category; I am writing a small program that is similar to tic tac toe. The game has 2 players enter 3 ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    Need help w/logical errors in array

    I am writing a small program that is similar to tic tac toe. The game has 2 players enter 3 positions each. (There is a total of 3 pieces for each player). If the positions on the board are vertical or horizontal it is considered a win. My problem is with my array where I store the board positions for each player to check for 3 in a row. It appears that my array for the human player is working properly, but not the array for the computer player. I copied the code from the human player function so I'm not sure why it won't work for the computer player. If someone could tell me what I'm doing wrong, I would greatly appreciate it. I'm ready to throw my computer out the window. I'm not very good at programming since I'm a beginner as you will see in my code below.

    Thanks in advance for your help.

    Code:
    /main.cpp
    
    #include <time.h>
    #include <iostream>
    using std::endl;
    using std::cin;
    using std::cout;
    
    #include <string>
    #include "human.h"
    #include "computer.h"
    #include "board.h"
    
    
    int main()
    {
    	UserInterface ui;//human player object
    	ComputerOpponent cp;//computer player object
    	Board bd;//board object
    
    	srand(time(NULL));//seed random number generator
    
    	int a, b, c, d, j, k;
    	bd.instructions();//print instructions
    	
    	for (j=1,k=1; j<=3,k<=3; j++,k++)
    	{
    		do {
    			ui.turn(a);// human move
    		} while (!bd.validTurn(a));
    			
    		bd.xturn(a);// store human move in array
    		bd.printBoard();// print board
    		bd.xStorePos(a,j);// store position for X (human)
    
    		do {
    			cp.turn(b);// computer move
    		} while (!bd.validTurn(b));
    				
    		bd.oturn(b);// store computer move in array
    		bd.printBoard();// print board
    		bd.oStorePos(b,j);// store position for O (computer)
    	}// for
    	
    	bd.sortXpos();// sorting X positions
    	bd.sortOpos();// sorting O positions
    	bd.printBoard();// print board
    	
    	if (bd.threeInaRow() ) {
    		cout << "Three in a row!  End of game\n\n" << endl;
    		exit(1);
    	}// if
    	
    //	do {
    		do {
    			ui.getMove(a,b,j);// ask human to enter to & from positions
    			bd.printBoard();// print board
    		} while (!bd.xvalidMove(a,b,j));	
    		
    		bd.getXpos(a,b);// replaces x array with new position
    		bd.xapplyMove(a,b);// apply valid moves
    		bd.printBoard();// print board
    		bd.xStorePos(b,j);// store positions for X (human)
    		bd.sortXpos();// sorting X positions
    						
    		if (bd.threeInaRow() ) {
    			cout << "\nThree in a row!  End of game\n\n" << endl;
    			exit(1);
    		}// if
    	
    		do {
    			cp.getMove(c,d,k);// ask computer to enter to & from positions
    			bd.printBoard();// print board
    		} while (!bd.ovalidMove(c,d,k));	
    
    		
    		// logical errors occuring in oStorepos or sortOpos (not sure) <------------
    		
    		bd.getOpos(c,d);// replaces o array with new position
    		bd.oapplyMove(c,d);// apply valid moves
    		bd.printBoard();// print board
    		bd.oStorePos(d,k); // store position for O (computer)
    		bd.sortOpos();// sorting O positions
    		
    		if (bd.threeInaRow() ) {
    			cout << "\nThree in a row!  End of game\n\n" << endl;
    			exit(1);
    		}// if
    	
    //	} while (!bd.threeInaRow());
    	
    	return 0;
    }
    
    //board.cpp
    #include "board.h"
    #include "swap.h"
    #include "computer.h"
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <string>
    
    Board::Board() {
    	for (int x=1; x<10; x++)
    	{
    		board[x]='\0';
    	}
    }// initialize board to null values
    
    void Board::printBoard() {
    	cout << "\t " << board[1] << "   " << board[2] << "   " << board[3] << endl;
    	cout << "\t" << "---+---+---" << endl;
    	cout << "\t " << board[4] << "   " << board[5] << "   " << board[6] << endl;
    	cout << "\t" << "---+---+---" << endl;
    	cout << "\t " << board[7] << "   " << board[8] << "   " << board[9] << endl;
    }// print board
    
    void Board::xturn(int &move) {
    	board[move]='X';
    }// print X's move on board 
    
    void Board::xStorePos(int &move, int j) {
    		x_position[j]=move;
    		cout << "\nxStorePos();" << endl;
    		cout << "index j = " << j << " x_position contents " << move << endl;
    }// stores X's position in array x_position
    
    void Board::oturn(int &move) {
    	board[move]='O';
    }// print O's move on board
    
    void Board::oStorePos(int &move,int k) {
    		o_position[k]=move;
    		cout << "\noStorePos();" << endl;
    		cout << "index k = " << k << " o_position contents " << move << "\n" << endl;
    }// stores O's position in array o_position
    
    void Board::sortXpos() {
    	int i=1;
    	if (x_position[i] > x_position[i+2]) swap (&x_position[i], &x_position[i+2]);
    	if (x_position[i+1] > x_position[i+2]) swap (&x_position[i+1],&x_position[i+2]);
    	if (x_position[i] > x_position[i+1]) swap (&x_position[i],&x_position[i+1]);
    	// cout statements for error checking
    	cout << "\n\n\n Printing x_position sorted array" << endl;
    	for (int x=1; x<=3; x++)
    	{
    		cout << "index x is " << x;
    		cout << "\tcontents of array position " << x_position[x] << endl;
    	}
    }// sort X's positions
    
    void Board::sortOpos() {
    	int j=1;
    	if (o_position[j] > o_position[j+2]) swap (&o_position[j], &o_position[j+2]);
    	if (o_position[j+1] > o_position[j+2]) swap (&o_position[j+1],&o_position[j+2]);
    	if (o_position[j] > o_position[j+1]) swap (&o_position[j],&o_position[j+1]);
    	//cout statements for error checking
    	cout << "\n\n\n Printing o_position sorted array" << endl;
    
    	for (int x=1; x<=3; x++) {
    		cout << "index x is " << x;
    		cout << "\tcontents of array position " << o_position[x] << endl;
    	}
    }// sort X's positions
     
    bool Board::threeInaRow() {
    	if ((x_position[1]==1 && x_position[2]==2 && x_position[3]==3)
    	   || (x_position[1]==4 && x_position[2]==5 && x_position[3]==6)
             || (x_position[1]==7 && x_position[2]==8 && x_position[3]==9) 
    		   || (o_position[1]==1 && o_position[2]==2 && o_position[3]==3)
    	         || (o_position[1]==4 && o_position[2]==5 && o_position[3]==6)
                   || (o_position[1]==7 && o_position[2]==8 && o_position[3]==9) 
    				 || (x_position[1]==1 && x_position[2]==4 && x_position[3]==7)
    				   || (x_position[1]==2 && x_position[2]==5 && x_position[3]==8)
    				     || (x_position[1]==3 && x_position[2]==6 && x_position[3]==9)
    					   || (o_position[1]==1 && o_position[2]==4 && o_position[3]==7)
    				         || (o_position[1]==2 && o_position[2]==5 && o_position[3]==8)
    				           || (o_position[17]==3 && o_position[2]==6 && o_position[3]==9))
    	
    	{
    	
    		return true;
    	}
    	else {
    		return false;
    	}
    }// threeInaRow
    
    void Board::swap (int *v1, int *v2) {
    	int temp = *v1;
    	*v1 = *v2;
    	*v2 = temp;
    }// swap function
    
    void Board::xapplyMove(int& from, int& to) {
    		board[from] = '\0';
    		board[to] = 'X';
    }// apply X's valid move
    
    void Board::oapplyMove(int& from, int& to) {
    		board[from] = '\0';
    		board[to] = 'O';
    }// apply O's valid move
    
    void Board::instructions() {
    	cout << "\n\nThank you for playing Three Men's Morris" << endl;
    	cout << "\nHow to play: " << endl;
    	cout << "Players take turns putting one marker at a time in any unoccupied box." << endl;
    	cout << "One player is X, the other is O.  There are only 6 markers." << endl;
    	cout << "If neither player has 3 in a row after all six markers have been placed," << endl; 
    	cout << "play continues.  The markers can be moved vertically or horizontally" << endl;
    	cout << "on intersection points during the game.  The winner is the firsst" << endl;
    	cout << "player to get three of their markers in a row." << endl;
    	cout <<	"\nPlease enter corresponding numbers on the grid below to place your positions.\n" << endl;
    	cout << "\t " << "1" << "   " << "2" << "   " << "3" << endl;
    	cout << "\t" << "---+---+---" << endl;
    	cout << "\t " << "4" << "   " << "5" << "   " << "6" << endl;
    	cout << "\t" << "---+---+---" << endl;
    	cout << "\t " << "7" << "   " << "8" << "   " << "9" << endl;
    	cout << "\n\nGood Luck!" << endl;
    }// instructions
    
    void Board::getXpos(int& from, int& to) {
    	int i=1;
    	cout << "getXpos () " << endl;  //for error checking
    	if (x_position[i]==from) {
    		x_position[i]=to;
    		cout << "x_position[i] is: " << x_position[i] << endl;
    	}
    	if (x_position[i+1]==from) {
    		x_position[i+1]=to;
    		cout << "x_position[i+1] is: " << x_position[i+1] << endl;
    	}
    	if (x_position[i+2]==from) {
    		x_position[i+2]=to;
    		cout << "x_position[i+2] is: " << x_position[i+2] << endl;
    	}
    }
    	
    void Board::getOpos(int& from, int& to) {
    	int i=1;
    	cout << "getOpos () " << endl; //for error checking
    	if (o_position[i]==from) {
    		o_position[i]=to;
    		cout << "o_position[i] is: " << o_position[i] << endl;
    	}
    	if (o_position[i+1]==from) {
    		o_position[i+1]=to;
    		cout << "o_position[i+1] is: " << o_position[i+1] << endl;
    	}
    	if (o_position[i+2]==from) {
    		o_position[i+2]=to;
    		cout << "o_position[i+2] is: " << o_position[i+2] << endl;
    	}
    }
    
    bool Board::validTurn(int& move) {
    	if (board[move] == '\0') {
    		return true;
    	}
    	else {
    		cout << "\nInvalid move!" << endl;
    		return false;
    	}
    }// check X & O for valid move
    
    bool Board::xvalidMove(int& from, int& to, int j) {
    	if (board[from] == 'X' && board[to] == '\0') {
    		return true;
    	}
    	else {
    		cout << "Invalid move!" << endl;
    		return false;
    	}
    }// check X for valid from/to move
    
    bool Board::ovalidMove(int& from, int& to, int k) {
    	if (board[from] == 'O' && board[to] == '\0') {
    		return true;
    	}
    	else {
    		cout << "Invalid move!" << endl;
    		return false;
    	}
    }// check O for valid from/to move
    
    //computer.cpp
    
    #include <stdlib.h>
    #include <string>
    #include <time.h>
    #include "Computer.h"
    #include "board.h"
    
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    void ComputerOpponent::getMove(int &from, int &to, int k)  // computer player
    {
    	cout << "\n\nComputer moving positions . . ." << endl;
    	cout << "Enter from:  "<< endl;
    	cin >> from;
    	cout << "Enter to: " << endl;
    	cin >> to;
    
    	/* implement code below when logical errors are fixed
    	
    	from=rand()% 9+1;
    	to=rand() % 9+1;
    	cout << "Attempting to move from position" << from << " to " << to << endl;	
    	*/
    
    }// get move
    
    ComputerOpponent::ComputerOpponent() {
    	 strcpy(name, "The Matrix");
    }
    
    void ComputerOpponent::setName(char *n) {
    	 cout << "\n\n" << endl;
    	 strcpy(name, n);
    	 cout << name << " has started!" << endl;
    }// set name
    
    void ComputerOpponent::turn(int &move)
    {
    	cout << "\nComputer enter position:  ";
    	cin >> move;
    	
    	/* implement code below when logical errors are fixed	
    	move = rand() % 9+1;
    	cout << "\nThe computer moved here . . . " << move << "\n\n";
    	return move;
    	*/
      
    }// get initial positions
    
    //human.cpp
    #include <iostream.h>
    #include <string>
    #include "human.h"
    #include "board.h"
    
    void UserInterface::getMove(int& from , int& to, int j) {
    	cout << "\n\nHuman enter moves please" << endl;
    	cout << "Enter from:  "<< endl;
    	cin >> from;
    	cout << "Enter to: " << endl;
    	cin >> to;
    } // get move
    
    void UserInterface::turn(int &move) {
    	cout << "\nHuman enter position:  ";
    	cin >> move;
    }// get initial positions
    
    ///board.h
    
    #ifndef _BOARD_H
    #define _BOARD_H
    
    class Board {
    public:
    	Board();// constructor
    	void printBoard();// prints board
    	void xturn(int &move);// human turn (X)
    	void oturn(int &move);// computer turn (O)
    	void xStorePos(int &move, int pos); // store X position in array
    	void oStorePos(int &move, int pos);// store O position in aray
    	void sortXpos();// sort X array
    	void sortOpos();// sort O array
    	void swap(int *v1, int *v2);// swap function
    	void xapplyMove(int& from, int& to);// apply X's valid move
    	void oapplyMove(int& from, int& to);// apply O's valid move
    	void instructions();// print instructions
    	void getXpos(int& from, int& to);// get from/to positions from human
    	void getOpos(int& from, int& to);// get from/to positions from computer
    	bool threeInaRow();// check for winner
    	bool validTurn(int& move);// validity check for 1st three initial positions
    	bool xvalidMove(int& from, int& to, int pos);// validity check for X's from/to positions
    	bool ovalidMove(int& from, int& to, int pos);// validity check for O's from/to positions
    
    private:
    	char board[10];
    	int x_position[3];
    	int o_position[3];
    };
    
    #endif
    
    //swap.h
    #ifndef _SWAP_H
    #define _SWAP_H
    
    void swap (int *, int *);  // "swap.h"
    
    #endif
    
    // human.h
    #ifndef _HUMAN_H
    #define _HUMAN_H
    
    class UserInterface {
    public:
    	void getMove(int& from, int& to, int j);
    	void turn(int &move);
    private:
      char playerName[30];
    };
    
    #endif
    
    //computer.h
    #ifndef _COMPUTER_H
    #define _COMPUTER_H
    
    class ComputerOpponent {
    public:
    	void getMove(int&from, int &to, int j);
    	int printMove(int&from, int &to);
    	char *getName() {return name;}
    	void setName(char *);
    	ComputerOpponent();
    	void turn(int &move);
    	void srand(unsigned int);
    private:
    	char name[30];
    };
    #endif

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    You know ... posting your whole game like that won't attract mucho help. I think most people here agree when I say that it will take less time to write a new tictack then to try to understand someone elses tictack. Narrow down the problem instead, and post again when there is < 20 rows of code to read.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    Sorry ninebit I thought I should post all my code. Here is the code where I am encountering errors. The function below is supposed to store 3 positions so I can check for a winner, but it doesn't seem to be working logically. It is an exact copy of my code for my other player which seems to be functioning properly. If anyone could help me it would be greatly appreciated. I've been looking at this code so long that I am not seeing my mistake. Can someone please help?

    function in board.cpp
    Code:
    void Board::sortOpos() {
    	int j=1;
    	if (o_position[j] > o_position[j+2]) swap (&o_position[j], &o_position[j+2]);
    	if (o_position[j+1] > o_position[j+2]) swap (&o_position[j+1],&o_position[j+2]);
    	if (o_position[j] > o_position[j+1]) swap (&o_position[j],&o_position[j+1]);
    	//cout statements for error checking
    	cout << "\n\n\n Printing o_position sorted array" << endl;
    
    	for (int x=1; x<=3; x++) {
    		cout << "index x is " << x;
    		cout << "\tcontents of array position " << o_position[x] << endl;
    	}
    }// sort X's positions
    function call in main
    Code:
    do {
    			cp.getMove(c,d,k);// ask computer to enter to & from positions
    			bd.printBoard();// print board
    		} while (!bd.ovalidMove(c,d,k));	
    
    		
    		// logical errors occuring in oStorepos or sortOpos (not sure) <------------
    		
    		bd.getOpos(c,d);// replaces o array with new position
    		bd.oapplyMove(c,d);// apply valid moves
    		bd.printBoard();// print board
    		bd.oStorePos(d,k); // store position for O (computer)
    		bd.sortOpos();// sorting O positions
    		
    		if (bd.threeInaRow() ) {
    			cout << "\nThree in a row!  End of game\n\n" << endl;
    			exit(1);
    		}// if
    Again, I followed the same procedure for the first player (which appears to be working). I cannot figure out where my program is going haywire. I know it has something to do with my sortOpos() function. Thanks in advancefor your help.

  4. #4
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Post errors?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    My errors happen when I try to replace a current value in the array with a new position.

    Currently stored in array.

    array[1] = 2
    array[2] = 4
    array[3] = 6

    After trying to replace arrray[1] = 2 to the value of 1 my new stored array is

    array[1] = 4
    array[2] = 6
    array[3] = 9

    Forgot to mention that after the positions are stored I sort them in ascending order so I can test for a winner.

    I'm not sure where the 9 would be coming from. I tried typing cout statements in my code, but I can't seem to find out where the error may be occuring. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  2. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 01:13 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21