java - How to call on the conditions defining a 'while' loop to define an 'if' statement inside said 'while' loop -


i finished writing java program game "tic tac toe" (i've been java-coding little on month , second biggest project far). works fine , everything; problem more 'cleaning up' code. main action in code big 'while' loop, start of involves ugly statement:

while (contains(player1selections, winningarray1) && contains(player1selections, winningarray2) &&  contains(player1selections, winningarray3) && contains(player1selections, winningarray4) &&   contains(player1selections, winningarray5) &&  contains(player1selections, winningarray6) && contains(player1selections, winningarray7) &&  contains(player1selections, winningarray8) && contains(player2selections, winningarray1) &&  contains(player2selections,winningarray2) &&  contains(player2selections,winningarray3) && contains(player2selections, winningarray4) &&  contains(player2selections, winningarray5) &&  contains(player2selections, winningarray6) && contains(player2selections, winningarray7) &&  contains(player2selections, winningarray8)) { 

to clarify, here checking if array list of player 1's co-ordinate selections or array list of player 2's co-ordinate selections contains of 8 hard-coded 3-elements-long arrays each possible victory pattern (which convert in boolean 'contains' method array lists can use 'containsall' method 2 array lists (i acquired idea stack overflow forum, surprise surprise)).

incidentally, requires mentioning set 'contains' method contains(player1selections, winningarray1) comes out true precisely when array list of player 1's selections does not contain array list of winning array 1 - that's why loop works (it stops when 1 player's selections contains winning array list). (sorry confusing name.)

so problem? problem when either player 1 or player 2 makes winning move, need phrase "player [x] wins!" come before final board printed, showing final positions of noughts , crosses (and when there's draw, need word "draw" come before final board printed), , way have solved super messy. main part of loop consists of code this:

    if (toggleplayer == 0) {     if (n1 == 0 && n2 == 0) {     if (coordinate00.equals(" ")) {    // coordinate00 string    coordinate00 = "x";    player1selections.add(0.0);          //the hard-coded winning-arrays consist of doubles in format    toggleplayer ++;}                       else { system.out.println("position occupied."); }   }                   // closing second 'if' statement not first, gets closed before start writing out similar stuff player 2 

and here have placed inside last part of loop (after statements 1 above), solution problem:

   if (contains(player1selections, winningarray1) &&   contains(player1selections, winningarray2) &&    contains(player1selections, winningarray3) &&     contains(player1selections, winningarray4) &&    contains(player1selections, winningarray5) &&      contains(player1selections, winningarray6) &&     contains(player1selections, winningarray7) &&     contains(player1selections, winningarray8) &&     contains(player2selections, winningarray1) &&      contains(player2selections,winningarray2) &&       contains(player2selections,winningarray3) &&     contains(player2selections, winningarray4) && c     contains(player2selections, winningarray5) &&   contains(player2selections, winningarray6) &&     contains(player2selections, winningarray7) &&       contains(player2selections, winningarray8)) {        if ((!coordinate00.equals(" ")) & (!coordinate10.equals(" ")) &          (!coordinate20.equals(" ")) & (!coordinate01.equals(" ")) &           (!coordinate11.equals(" ")) &      (!coordinate21.equals(" ")) & (!coordinate02.equals(" ")) &       (!coordinate12.equals(" ")) & (!coordinate22.equals(" "))) {       system.out.println("draw");       system.out.println("\n" + coordinate00 + "|" + coordinate10 + "|" + coordinate20 + "\n" + "-----" + "\n" + coordinate01 + "|" + coordinate11 + "|" + coordinate21 + "\n" +"-----" + "\n" + coordinate02 + "|" + coordinate12 + "|" + coordinate22 + "\n");       return;   }   else { system.out.println("\n" + coordinate00 + "|" + coordinate10 + "|" + coordinate20 + "\n" + "-----" + "\n" + coordinate01 + "|" + coordinate11 + "|" + coordinate21 + "\n" +"-----" + "\n" + coordinate02 + "|" + coordinate12 + "|" + coordinate22 + "\n"); 

evidently, stupid thing last bit of code i've literally had repeat verbatim 'while' loop conditions 'if' statement, though 'if' statement still inside 'while' loop itself. had this, since couldn't figure out more concise way of making sure program prints board straight away if game isn't ending.

now, have idea how make less messy, don't know if it's possible... basically, i'm wondering if there's way to'call on' earlier 'while' loop conditions instead of writing them out again 'if' statement. want 'the conditions if statement same 'while' loop still in effect.'

this question may unanswerable, knowing impossible thing useful.

make "winningarray1" through "winningarray8" list of arrays. let's call list "winningmoves". if player2selections own custom class, can add "contains" function iterates through whole list. loop more like:

while (player1selections.contains(winningmoves)      || player2selections.contains(winningmoves)) { ... } 

some board games can simplified representing board single string, using string operations board.contains(" ") instead of spelling out each coordinate.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -