Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 9:01am On Oct 02, 2017 |
I started learning computer programming recently(about two months ago) on my own...So,i am still doing the very basic stuffs...I wanted to write a working code with what i have learnt...So,i decided to write a tic-tac-toe game(no GUI),as to that , i wrote the code bellow...The code works but it has a very big problem and I have been trying to fix it since yesterday,i couldn't.....The problem is that when there is a win,it mistakes the winner always..if Tobi and Dele are the players,when Tobi wins, it will give the win to Dele,just like that.... I know the documentation is poor (remember I am a beginner). Help me fix this.. //tic-tac-toe game
import java.util.Scanner ;
class Tic_Tac_Toe{ private static final int ROW = 3 ; private static final int COLUMN = 3 ; private String frame ; private char choice1 ; private char choice2 ; //create a variable access to a Player Instance Player player1 ; Player player2 ; char[][] Game_Frame ; //various states possible during the game play private enum Winner {PLAYER1 , PLAYER2 , CONTINUE} ; //switches between the character entered by player1 and player2 private enum Turn{ player1 , player2 }; /** * Constructs an instance of the game *creates an object of the players *@param p1 ,p2 the player names @param c1 , c2 the parameter each of the players will like to use for the game */ public Tic_Tac_Toe(String p1 , char c1 , String p2 , char c2){ choice1 = c1 ; choice2 = c2 ; Game_Frame = new char[ROW][COLUMN] ; player1 = new Player(p1 , choice1 ) ; player2 = new Player(p2 , choice2) ; System.out.println("Welcome " +player1.name +" and " + player2.name + " to the tic-tac-toe game ,fully designed by Lucasiangrey" ; } //shows an empty frame public String showFrame(){ frame = "| | | | \n| | | | \n| | | |"; return frame ;
} //create an instance of the enumeration Turn turn ; //takes an input of position to play and prints the board afterward public String playAndDisplayBoard(int r,int c ){ if((r< 1 || r>3)||(c < 1 || c >3)){ System.out.println("Wrong move,Try another " ; return "This, is the beta version of the game...the game may mis - act "; } if(turn == Turn.player1){ Game_Frame[r-1][c-1] = choice1 ; turn = Turn.player2 ; } else { Game_Frame[r-1][c-1] = choice2 ; turn = Turn.player1 ; } //frame after entries frame = Game_Frame[0][0] + " | " + Game_Frame[0][1] + " | " + Game_Frame[0][2] +"\n" + Game_Frame[1][0] + " | " + Game_Frame[1][1] + " | " + Game_Frame[1][2] +"\n" + Game_Frame[2][0] + " | " + Game_Frame[2][1] + " | " + Game_Frame[2][2] ; isAWin() ; //return the frame return frame ; } //checks for a win for each turn taken by player Winner winner ; public boolean isAWin(){ if(Game_Frame[0][0]==choice1 && Game_Frame[0][1] == choice1 && Game_Frame[0][2] == choice1){ winner = Winner.PLAYER1 ; gameOver(player1); return true ; } else if (Game_Frame[2][0]==choice1 && Game_Frame[2][1] == choice1 && Game_Frame[2][2] == choice1){ winner = Winner.PLAYER1 ; gameOver(player1); return true ; } else if(Game_Frame[0][0]==choice1 && Game_Frame[1][0] == choice1 && Game_Frame[2][0] == choice1){ winner = Winner.PLAYER1 ; gameOver(player1) ; return true ; } else if (Game_Frame[0][2]==choice1 && Game_Frame[1][2] == choice1 && Game_Frame[2][2] == choice1){ winner = Winner.PLAYER1 ; gameOver(player1); return true ; } // else if(Game_Frame[0][0]==choice1 && Game_Frame[1][1] == choice1 && Game_Frame[2][2] == choice1){ winner = Winner.PLAYER1 ; gameOver(player1); return true ; } else if(Game_Frame[2][ 0]==choice1 && Game_Frame[1][1] == choice1 && Game_Frame[0][2] == choice1){ winner = Winner.PLAYER1 ; gameOver(player1) ; return true ; } else if(Game_Frame[0][1]==choice1 && Game_Frame[1][1] == choice1 && Game_Frame[2][1] == choice1){ winner = Winner.PLAYER1 ; gameOver(player1); return true ; } else if(Game_Frame[1][0]==choice1 && Game_Frame[1][1] == choice1 && Game_Frame[1][2] == choice1){ winner = Winner.PLAYER1 ; gameOver(player1); return true ; } else if(Game_Frame[0][0]==choice2 && Game_Frame[1][1] == choice2 && Game_Frame[2][2] == choice2){ winner = Winner.PLAYER2 ; gameOver(player2); return true ; } else if(Game_Frame[2][0]==choice2 && Game_Frame[1][1] == choice2 && Game_Frame[0][2] == choice2){ winner = Winner.PLAYER2 ; gameOver(player2) ; return true ; } else if(Game_Frame[0][1]==choice2 && Game_Frame[1][1] == choice2 && Game_Frame[2][1] == choice2){ winner = Winner.PLAYER2 ; gameOver(player2) ; return true ; } else if(Game_Frame[1][0]==choice2 && Game_Frame[1][1] == choice2 && Game_Frame[1][2] == choice2){ winner = Winner.PLAYER2 ; gameOver(player2); return true ; } // else if(Game_Frame[0][0]==choice2 && Game_Frame[0][1] == choice2 && Game_Frame[0][2] == choice2){ winner = Winner.PLAYER2 ; gameOver(player2); return true ; } else if (Game_Frame[2][0]==choice2 && Game_Frame[2][1] == choice2 && Game_Frame[2][2] == choice2){ winner = Winner.PLAYER2 ; gameOver(player2); return true ; } else if(Game_Frame[0][0]==choice2 && Game_Frame[1][0] == choice2 && Game_Frame[2][0] == choice2){ winner = Winner.PLAYER2 ; gameOver(player2) ; return true ; } else if (Game_Frame[0][2]==choice2 && Game_Frame[1][2] == choice2 && Game_Frame[2][2] == choice2){ winner = Winner.PLAYER2 ; gameOver(player2); return true ; } else { winner = Winner.CONTINUE ; return false ; } } //print the gameOver message public void gameOver(Player p){ System.out.println(p.name +" is the winner " } }
//the player class ...
class Player { public String name ; private char charaterOfChoice ; /** Constructs the a Player Object @param character the character for play associted to a particular player @param name the name of a particular player */ public Player(String n,char ch){ name = n ; charaterOfChoice = ch ; } }
//a test on the app public class TicApp { public static void main (String[] arg){ //create an instance of the class Tic_Tac_Toe Tic_Tac_Toe GAME ; //create an instance of the scanner Scanner scan = new Scanner (System.in); //prompt the players to enter there character of choice System.out.print("First player should enter his/her name :" ; String name1 = scan.next() ; System.out.println(); System.out.print("FIrst player should enter his/her character :" ; char ch1 = (char)scan.nextInt() ; System.out.println() ; System.out.print("The second player should enter his/her name :" ; String name2 =scan.next() ; System.out.println() ; System.out.print("The second player should enter his/her character" ; char ch2 = (char)scan.nextInt() ; System.out.println() ; //pass the parameter to the Tic_Tac_Toe constructor GAME = new Tic_Tac_Toe(name1 , ch1 , name2 , ch2 ) ; //print the empty frame System.out.println("Rules of The GAME" System.out.println("*Enter the row and then the column you want to place your character" GAME.showFrame() ; System.out.println() ; //looping through the gaming process System.out.println(GAME.showFrame()) ; System.out.println("Let,the game begin "+name1 +" ,please start the game" ; System.out.println(); System.out.println(); int counter = 1 ; int row ; int column ; while(GAME.isAWin()== false){ if(counter%2 == 1){ System.out.print(name1 + ",enter the row " ; row = scan.nextInt () ; System.out.println(); System.out.println(name1 +" enter the column " ; column = scan.nextInt() ; System.out.println(GAME.playAndDisplayBoard(row , column ) ); counter++ ; } else { System.out.println(name2 +" Enter the row " row = scan.nextInt () ; System.out.println() ; System.out.println("Enter the column " ; column = scan.nextInt() ; System.out.println(GAME.playAndDisplayBoard (row , column )) ; counter++ ; } } } }
|
Re: Java Programmers ,I Need Help Fixing This Code. by yorex2011: 9:02am On Oct 02, 2017 |
Wtf is this, no explanation of problem, nothing, just dropped code... Na wa o |
Re: Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 9:17am On Oct 02, 2017 |
yorex2011: Wtf is this, no explanation of problem, nothing, just dropped code... Na wa o I am really Sorry about that...was not done...just modified it. |
Re: Java Programmers ,I Need Help Fixing This Code. by Jacktheripper: 9:40am On Oct 02, 2017 |
Leave the programming world u lameass |
Re: Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 9:44am On Oct 02, 2017 |
Jacktheripper: Leave the programming world u lameass k...like you were never once a beginner...thanks for the comment anyway. |
Re: Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 10:05am On Oct 02, 2017 |
Javanian
WhiZTiM |
Re: Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 11:34am On Oct 02, 2017 |
If you know of people who can help,please give them mention ...i couldn't move forward in my learning due to this... |
Re: Java Programmers ,I Need Help Fixing This Code. by WhiZTiM(m): 2:34pm On Oct 02, 2017 |
Jacktheripper: Leave the programming world u lameass @OP, don't mind this one... He's prolly high on some rare stuff. Stop using nairaland's code feature for long codes... Its crappy... really buggy... Use some other online code pasting services. BTW, where and what is the definition of the function "checkWin();" ...? 2 Likes |
Re: Java Programmers ,I Need Help Fixing This Code. by ToyinDipo(m): 4:32pm On Oct 02, 2017 |
You didn't initialize turn, so it's the second player always playing first, instead of the intended first player.
The code need some refactoring, but here are the simple ones:
method checkWin should take in a parameter that holds the player who played last, so that you won't need to duplicate the if else blocks for each player
Before making a call to the checkWin method, you should test for the number of moves played, if it's not up to 5, then there is no need
Also, you could wrap the check for the non diagonal lines thus:
for (int i=0; i<=2; i++) { for (int r =0; r<=2; r++) { if (games[i, 0] == games[i, 1] && games[i, 0] == games[i, 2]) { return true; } if (games[0, i] == games[1, i] && games[0, i] == games[2, i]) { return true; } } } 1 Like |
Re: Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 5:18pm On Oct 02, 2017 |
WhiZTiM:
@OP, don't mind this one... He's prolly high on some rare stuff.
Stop using nairaland's code feature for long codes... Its crappy... really buggy... Use some other online code pasting services. BTW, where and what is the definition of the function "checkWin();" ...? I don't know how to use any other code feature ...in fact i don't know what exactly that mean. ..modified It.... is meant to be isAWin()...I used checkWin when i wrote he code...but when i was about posting it here,I had to change the name of the boolean method so it reflect its purpose better (just to avoid insults ). Just so you know,you inspire me a lot...though,I may not be participating in this programming section but believe me,you made me get a laptop(you made me understand even b4 learning programming that writing a working code is different from writing an efficient code ...i saw your contribution and a guy called josh to the thread about solving project euler's problems)... |
Re: Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 5:41pm On Oct 02, 2017 |
ToyinDipo: You didn't initialize turn, so it's the second player always playing first, instead of the intended first player.
The code need some refactoring, but here are the simple ones:
method checkWin should take in a parameter that holds the player who played last, so that you won't need to duplicate the if else blocks for each player
Before making a call to the checkWin method, you should test for the number of moves played, if it's not up to 5, then there is no need
Also, you could wrap the check for the non diagonal lines thus:
for (int i=0; i<=2; i++) { for (int r =0; r<=2; r++) { if (games[i, 0] == games[i, 1] && games[i, 0] == games[i, 2]) { return true; } if (games[0, i] == games[1, i] && games[0, i] == games[2, i]) { return true; } } }
CODE FIXED!...thanks a lot bro...that loop you gave is a far more concise code(took about 3minutes to understand )...Thanks...But,how good is my code on a scale of 0 to 5 ?. |
Re: Java Programmers ,I Need Help Fixing This Code. by ToyinDipo(m): 7:23pm On Oct 02, 2017 |
Lucasiangrey:
CODE FIXED!...thanks a lot bro...that loop you gave is a far more concise code(took about 3minutes to understand )...Thanks...But,how good is my code on a scale of 0 to 5 ?. You are welcome. For a beginner, 4/5 1 Like |
Re: Java Programmers ,I Need Help Fixing This Code. by WhiZTiM(m): 5:57pm On Oct 06, 2017 |
Lucasiangrey:
I don't know how to use any other code feature ...in fact i don't know what exactly that mean.
..modified It.... is meant to be isAWin()...I used checkWin when i wrote he code...but when i was about posting it here,I had to change the name of the boolean method so it reflect its purpose better (just to avoid insults ).
Just so you know,you inspire me a lot...though,I may not be participating in this programming section but believe me,you made me get a laptop(you made me understand even b4 learning programming that writing a working code is different from writing an efficient code ...i saw your contribution and a guy called josh to the thread about solving project euler's problems)... Wow .. Thank you for the kind compliments. This, and the apparent evidence for your passion has motivated me to go a step further with your project... So, lets attempt improve your work... First, what JDK version are you using? ...or which IDE are you using? And what version? I simply want to confirm whether you can use Java 8 features. |
Re: Java Programmers ,I Need Help Fixing This Code. by asibedaniel: 11:13am On Oct 07, 2017 |
Please can anyone help me with CSS I don't know how to space objects |
Re: Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 10:22pm On Oct 09, 2017 |
WhiZTiM:
Wow .. Thank you for the kind compliments.
This, and the apparent evidence for your passion has motivated me to go a step further with your project... So, lets attempt improve your work...
First, what JDK version are you using? ...or which IDE are you using? And what version?
I simply want to confirm whether you can use Java 8 features. thanks,sir...i am using jdk1.7. |
Re: Java Programmers ,I Need Help Fixing This Code. by WhiZTiM(m): 11:42am On Oct 10, 2017 |
|
Re: Java Programmers ,I Need Help Fixing This Code. by Lucasiangrey: 3:22am On Oct 13, 2017 |
WhiZTiM:
Ok. Can you install JDK1.8? You can download the Netbeans 8.2 and Java8 bundle here: http://www.oracle.com/technetwork/java/javase/downloads/jdk-netbeans-jsp-142931.html
Once you do so, buzz me. I have installed jdk1.8 and netbeans sir.I have been using notepad ++ , i don't think i know how to use this netbeans well,yet.if it's not a necessity, i will like to just continue with the notepad ++ otherwise , i can dedicate few days to practice code and learn some features of that IDE...thanks. |
Re: Java Programmers ,I Need Help Fixing This Code. by HottestFire: 7:30pm On Oct 13, 2017 |
Lucasiangrey:
I have installed jdk1.8 and netbeans sir.I have been using notepad ++ , i don't think i know how to use this netbeans well,yet.if it's not a necessity, i will like to just continue with the notepad ++ otherwise , i can dedicate few days to practice code and learn some features of that IDE...thanks.
As a self taught programmer, playing around the IDE shouldn't be hard...You've tried.
|