Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Tic Tac Toe - Checking for a winner

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tic Tac Toe - Checking for a winner

    Hello!

    I am currently developing a little Tic Tac Toe game in Java (For Android)
    And i am stuck, i want to "CheckForWinner();" but i can't really figure out how...

    I am thinking something like: "foreach(Field in Fields)" and then check which has value of 1 and which has a value of 2 :-)


    EDIT: I figured it out now, and i did like this:

    public boolean isWon(int token) {
    		for (int i = 0; i < 3; i++)
    			if ((gameBoard[i][0] == token) && (gameBoard[i][1] == token)
    					&& (gameBoard[i][2] == token)) {
    				return true;
    			}
     
    		for (int j = 0; j < 3; j++)
    			if ((gameBoard[0][j] == token) && (gameBoard[1][j] == token)
    					&& (gameBoard[2][j] == token)) {
    				return true;
    			}
     
    		if ((gameBoard[0][0] == token) && (gameBoard[1][1] == token)
    				&& (gameBoard[2][2] == token)) {
    			return true;
    		}
     
    		if ((gameBoard[0][2] == token) && (gameBoard[1][1] == token)
    				&& (gameBoard[2][0] == token)) {
    			return true;
    		}
     
    		return false;
    	}

    Thanks in advance!

    - Frederik
    Last edited by FrederikB; April 4th, 2013 at 11:03 AM. Reason: Updated post


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Can you use an array for the board instead of 9 different variables? The array can hold references to the Field objects. That will reduce the number of if tests needed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    I probably could, but i dont know how

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Can you explain where the problem is?
    Defining an array
    assigning values to the slots in an array
    referencing the contents of a slot in an array
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    I am a little new to Java and haven't worked alot with arrays..
    Please explain more and show some code :-)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Here is the tutorial for arrays:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    I suggest you put the current code aside for now and write a simple program with arrays to learn how to use them.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    I have used arrays before, not just alot so you want me to do like this?:

    int[] gameBoard;
    gameBoard = new int[9];

    And then:
    gameBoard[0] = 1;
    To show that the field has been taken by Player 1?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Or use an array of Field objects:
    Field[] theSquares = new Field[9];
    Then assign each slot with a Field object;

    Or define the array with existing the existing Field objects:
    Field[] theSquares = {row1_field1, row1_field2, etc through 3 3};
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    I changed my sourcecode, how would i now check for a winner?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    I don't think you are using the array like an array. The code is using the array's slots like 9 different variables with the same name and a unique suffix like: [5]

    Try using a 2 dim array that you can use the row and field values as indexes into the array instead of that awful bunch of if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Please explain more

    - And with some code examples

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Look at something like this:
    if(gameboard[row][field] == 0) {
      theField[row][field].setImageDrawable(GetPlayer());
      gameboard[row][field] = curPlayer;
      changePlayer(); 
    }
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    How would i create a gameBoard array with two []? (not sure what they are called.. )

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    int[][] gameboard = new int[3][3]; // define 2 dim gameboard array
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Okay, now i have this :-)

    private void ClickField(int row, int field) {
     
    		if (gameBoard[row][field] == 0) {
    			theField[row][field].img.setImageDrawable(GetPlayer());
    			gameBoard[row][field] = curPlayer;
    			ChangePlayer();
    		}
     
    		CheckForWinner();
     
    	}

    So how will i do the "theField[row][field].img.setImageDrawable(GetPlayer());"?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Define a 2 dim array: theField and fill it with ImageView objects.
    It replaces the 9 variables like this: row1_field1_img

    You may have to change the indexes to be 0 based: 0-2 vs 1-3
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    Okay! I got it all working now!
    So far so good, how will i now check for a winner?

    Updated first post with updated sourcecode

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    if (row == 1) {
    			row = 0;
    		}
    How about: row = row - 1

    If you can read Javascript, here's one way to do it:
    http://normsstuff.zxq.net/Games/TicTacToe.html#top
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe - Checking for a winner

    I have it working now with this code:

    public boolean isWon(int token) {
    		for (int i = 0; i < 3; i++)
    			if ((gameBoard[i][0] == token) && (gameBoard[i][1] == token)
    					&& (gameBoard[i][2] == token)) {
    				return true;
    			}
     
    		for (int j = 0; j < 3; j++)
    			if ((gameBoard[0][j] == token) && (gameBoard[1][j] == token)
    					&& (gameBoard[2][j] == token)) {
    				return true;
    			}
     
    		if ((gameBoard[0][0] == token) && (gameBoard[1][1] == token)
    				&& (gameBoard[2][2] == token)) {
    			return true;
    		}
     
    		if ((gameBoard[0][2] == token) && (gameBoard[1][1] == token)
    				&& (gameBoard[2][0] == token)) {
    			return true;
    		}
     
    		return false;
    	}


    Thanks for all the help!!

    /solved

Similar Threads

  1. Tic Tac Toe problem help!
    By vess28 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 29th, 2012, 06:59 PM
  2. Tic-Tac-Toe Help
    By coke32 in forum Object Oriented Programming
    Replies: 13
    Last Post: March 12th, 2012, 07:59 AM
  3. TIC-TAC-TOE GAME
    By umerahmad in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 29th, 2011, 12:15 PM
  4. [SOLVED] Tic-Tac-Toe program
    By Actinistia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2011, 11:18 PM
  5. [SOLVED] Tic Toe java code exists when we enter 0 row and 0 column
    By big_c in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:16 AM