Results 1 to 17 of 17
- 05-19-2012, 06:53 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Tic-Tac-Toe Version.1.0. Requesting feedback and help.
I've started a new project called Tic-Tac-Toe, it was suggested as an exercise in my programming book. I realise this is actually a fine project because its extensible and I want to see how far I can take it. I intend to add a AI, GUI, error handling, export score to file, network play (sockets) etc. (I havent learned these things yet, but I will with the coming chapters in my book.
At the moment I've been working on version 1. the goal here is a simple player 1 vs player 2 console game. I would like the experts here to critique the code and suggest any refactoring, like perhapes I should seperate data between classes x, y... (at the moment its one class) advice like this would be helpful.
There is one immediate error I need help with, afterJava Code:package chapter_viii; import java.util.Scanner; public class TicTacToe_8_18 { private enum value { O, X, EMPTY }; private static final int O = 0; private static final int X = 1; private static final int EMPTY = 2; private Scanner input = new Scanner(System.in); private Scanner inputLine = new Scanner(System.in); private value player1; private value player2; private int player1Score; private int player2Score; private int drawScore; private value[][] board = new value[3][3]; public TicTacToe_8_18() { //constructor code no longer needed with boardReset } public void boardReset() { for( int row = 0 ; row < board.length ; row++ ) { for( int column = 0 ; column < board[row].length ; column++) { board[row][column] = value.EMPTY; } } } public void setPlayerSymbol() { int playerChoice; System.out.println("What symbol will player one use? (1 = O, 2 = X)"); playerChoice = input.nextInt(); if( playerChoice == 1 ) { player1 = value.O; player2 = value.X; } else // player == 2 { player1 = value.X; player2 = value.O; } } public int addValue(int row, int column, value val) { if( board[row][column] == value.EMPTY) { board[row][column] = val; return 1; } else { return -1; } } public int checkComplete() { int complete = 0; // XXX // --- // --- if( (board[0][0] == value.O && board[0][1] == value.O && board[0][2] == value.O) || (board[0][0] == value.X && board[0][1] == value.X && board[0][2] == value.X) ) { complete = 1; } // --- // XXX // --- if( (board[1][0] == value.O && board[1][1] == value.O && board[1][2] == value.O) || (board[1][0] == value.X && board[1][1] == value.X && board[1][2] == value.X) ) { complete = 1; } // --- // --- // XXX if( (board[2][0] == value.O && board[2][1] == value.O && board[2][2] == value.O) || (board[2][0] == value.X && board[2][1] == value.X && board[2][2] == value.X) ) { complete = 1; } // X-- // X-- // X-- if( (board[0][0] == value.O && board[1][0] == value.O && board[2][0] == value.O) || (board[0][0] == value.X && board[1][0] == value.X && board[2][0] == value.X) ) { complete = 1; } // -X- // -X- // -X- if( (board[0][1] == value.O && board[1][1] == value.O && board[2][1] == value.O) || (board[0][1] == value.X && board[1][1] == value.X && board[2][1] == value.X) ) { complete = 1; } // --X // --X // --X if( (board[0][2] == value.O && board[1][2] == value.O && board[2][2] == value.O) || (board[0][2] == value.X && board[1][2] == value.X && board[2][2] == value.X) ) { complete = 1; } // X-- // -X- // --X if( (board[0][0] == value.O && board[1][1] == value.O && board[2][2] == value.O) || (board[0][0] == value.X && board[1][1] == value.X && board[2][2] == value.X) ) { complete = 1; } // --X // -X- // X-- if( (board[0][2] == value.O && board[1][1] == value.O && board[2][0] == value.O) || (board[0][2] == value.X && board[1][1] == value.X && board[2][0] == value.X) ) { complete = 1; } // XXX // XXX // XXX if( board[0][0] != value.EMPTY && board[0][1] != value.EMPTY && board[0][2] != value.EMPTY && board[1][0] != value.EMPTY && board[1][1] != value.EMPTY && board[1][2] != value.EMPTY && board[2][0] != value.EMPTY && board[2][1] != value.EMPTY && board[2][2] != value.EMPTY) { complete = -1; } return complete; } public void turn(value player) { int playerSelection; int legal = 0; while(legal != 1) { System.out.println("Please select a location"); playerSelection = input.nextInt(); switch(playerSelection) { case 1: legal = addValue(0,0, player); break; case 2: legal = addValue(0,1, player); break; case 3: legal = addValue(0,2, player); break; case 4: legal = addValue(1,0, player); break; case 5: legal = addValue(1,1, player); break; case 6: legal = addValue(1,2, player); break; case 7: legal = addValue(2,0, player); break; case 8: legal = addValue(2,1, player); break; case 9: legal = addValue(2,2, player); break; } if( legal == -1 ) { System.out.println("Invalid move"); } } } public void postGame(int victory) { String playAgain; System.out.println("The game is over!"); if( victory != -1) //there is a winner { System.out.println("The winner is player " + victory); if( victory == 1 ) //player1 wins { player1Score++; } else //victory == 2 player2 wins { player2Score++; } } else //victory == -1 draw { System.out.println("Its a draw"); drawScore++; } System.out.println("Player 1: " + player1Score + " Player 2: " + player2Score + " draws: " + drawScore); System.out.println("Do you want to play again? (y/n)"); playAgain = inputLine.nextLine(); if( (playAgain == "y") || (playAgain == "Y") || (playAgain == "yes") || (playAgain == "Yes") || (playAgain == "YES") ) { play(); } else if( (playAgain == "n") || (playAgain == "N") || (playAgain == "no") || (playAgain == "No") || (playAgain == "NO") ) { System.out.println("Have a nice day"); } } public void play() { int victory = 0; boardReset(); System.out.println(this); setPlayerSymbol(); while (true) { this.turn(player1); System.out.println(this); if( checkComplete() == 1 ) { victory = 1; break; } if( checkComplete() == -1 ) { victory = -1; break; } this.turn(player2); System.out.println(this); if( checkComplete() == 1 ) { victory = 2; break; } if( checkComplete() == -1 ) { victory = -1; break; } } postGame(victory); } public String toString() { String string = ""; for( int row = 0 ; row < board.length ; row++ ) { for( int column = 0 ; column < board[row].length ; column++) { System.out.print(String.format("%-5s", board[row][column]) + " "); } System.out.println(); } return string; } public static void main(String[] args) { TicTacToe_8_18 ticTacToe = new TicTacToe_8_18(); ticTacToe.play(); } }
The program stops after this I dont know why, I created another Scanner to handle that as a debuging attempt but that wasnt the problem, perhapes there is something wrong with the following if statement?. observe output.Java Code:System.out.println("Do you want to play again? (y/n)"); playAgain = inputLine.nextLine();
Java Code:EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY What symbol will player one use? (1 = O, 2 = X) 1 Please select a location 1 O EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY Please select a location 2 O X EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY Please select a location 3 O X O EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY Please select a location 4 O X O X EMPTY EMPTY EMPTY EMPTY EMPTY Please select a location 5 O X O X O EMPTY EMPTY EMPTY EMPTY Please select a location 6 O X O X O X EMPTY EMPTY EMPTY Please select a location 7 O X O X O X O EMPTY EMPTY The game is over! The winner is player 1 Player 1: 1 Player 2: 0 draws: 0 Do you want to play again? (y/n) y
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-20-2012, 06:32 PM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
I figured it out, you cant test for string equality using "==" I think this is because its testing whether its the same String object (am I wrong?) I fixed the code with the equals method. Surprised no one else spotted that, or perhapes you wanted me to figure it out. I would still like some advice with my project... I was hoping to do some research and analaysis by listening to how you guys would go about making a tic-tac-toe game.
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-20-2012, 06:42 PM #3
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
Lines 75 to 150 looks very repetitive. Can you change that to use an array in a loop?
If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 12:54 AM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
I'm finding this very difficult to visualise... would I need 3 loops for each mark but that wouldnt work because I would still need a list of if statements to determine what the outer loops were... because the importantce of the inner loops is determined by the outloops (where the other marks are).
I could loop through each element while including a list of exceptions but then I've just changed from a list of victory conditions to a list of loosing conditions...
I'm sorry I'm not smart enough to see what you're saying here. could you elaberateLegend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-21-2012, 01:26 AM #5
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
My version of the board used a one dim array. That allowed a two dim array 8x3 to specify all the winning combinations.
http://normsstuff.zxq.net/Games/TicTacToe.htmlLast edited by Norm; 05-21-2012 at 01:29 AM.
If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 03:35 AM #6
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
what a nice program well done good sir, not perfect though. I selected the last remaining position centre top and I get this
"Did not find your move? Click on button in square before making move"
after I select make
I tried reselecting the last square but the program wont advance.Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-23-2012, 04:15 AM #7
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
Guys I need your help resolving this issue for me. I have decided to saturate my program into 4 classes ( NaughtsAndCrosses, Board, Player and GUI (coming soon) )
I havent made many programs with multiple classes so when I went back through my program and sorted it into classes I got myself into a logic problem. can you guys help me resolve this issue?
the problem is as you will see, I have two enum values[] one for the player (O, X) and one for the board (O, X, EMPTY), now I have a method I use to add the players value on to the board but ofcourse the board only takes its own enum values[]. headache! how do I mend this?
new code:
NaughtsAndCrosses
PlayerJava Code:package naughtsAndCrosses; import java.util.InputMismatchException; import java.util.Scanner; public class NaughtsAndCrosses { Scanner input = new Scanner(System.in); Board board = new Board(); Player player1 = new Player(); Player player2 = new Player(); public void turn(Player player) { int playerSelection; int legal; System.out.println(player + " Please select a location"); do { legal = 0; try { playerSelection = input.nextInt(); switch(playerSelection) { case 1: legal = board.addValue(0,0, player.getPlayerSymbol()); break; case 2: legal = board.addValue(0,1, player.getPlayerSymbol()); break; case 3: legal = board.addValue(0,2, player.getPlayerSymbol()); break; case 4: legal = board.addValue(1,0, player.getPlayerSymbol()); break; case 5: legal = board.addValue(1,1, player.getPlayerSymbol()); break; case 6: legal = board.addValue(1,2, player.getPlayerSymbol()); break; case 7: legal = board.addValue(2,0, player.getPlayerSymbol()); break; case 8: legal = board.addValue(2,1, player.getPlayerSymbol()); break; case 9: legal = board.addValue(2,2, player.getPlayerSymbol()); break; default: { System.out.println("Invalid option, please select 1-9"); } } if( legal == -1 ) { System.out.println("Invalid move, already taken. Please select" + " again (1-9)"); } } catch(InputMismatchException inputMismatchException) { System.err.println("Invalid input, please select (1-9)"); input.nextLine(); } } while (legal != 1); } public void postGame(int winner) { String playAgain; boolean reselect; System.out.println("The game is over!"); if( winner != -1) //there is a winner { System.out.println("The winner is player " + winner); if( winner == 1 ) //player1 wins { player1.addScore(1); player2.addScore(-1); } else //victory == 2 player2 wins { player2.addScore(1); player1.addScore(-1); } } else //victory == -1 draw { System.out.println("Its a draw"); player1.addScore(0); player2.addScore(0); } System.out.println("Do you want to play again? (y/n)"); do { try { reselect = false; playAgain = input.nextLine(); if( playAgain.equals("y") || playAgain.equals("Y") || playAgain.equals("yes") || playAgain.equals("Yes") || playAgain.equals("YES") ) { play(); } else if( playAgain.equals("n") || playAgain.equals("N") || playAgain.equals("no") || playAgain.equals("No") || playAgain.equals("NO") ) { System.out.println("Have a nice day"); System.exit(0); } else { System.out.println("Invalid option, please select y/n"); reselect = true; } } catch(InputMismatchException inputMismatchException) { System.err.println("Invalid input, please select y/n"); } } while ( reselect = true); } public void play() { int winner = 0; board.boardReset(); System.out.println(board.toString()); player1.requestPlayerSymbol(); if( player1.getPlayerSymbol() == Player.O() ) { player2.setPlayerSymbol('X'); } else { player2.setPlayerSymbol('O'); } while (true) { turn(player1); System.out.println(board); if( board.checkGameWon() == 1 ) { winner = 1; break; } if( board.checkGameWon() == -1 ) { winner = -1; break; } turn(player2); System.out.println(board); if( board.checkGameWon() == 1 ) { winner = 2; break; } if( board.checkGameWon() == -1 ) { winner = -1; break; } } postGame(winner); } public static void main(String[] args) { NaughtsAndCrosses game = new NaughtsAndCrosses(); game.play(); } }
BoardJava Code:package naughtsAndCrosses; import java.util.InputMismatchException; import java.util.Scanner; public class Player { private enum value { O, X, }; private static final int O = 0; private static final int X = 1; private value playerSymbol; private int wins; private int loses; private int draws; public void addScore(int score) { if( score == 1 ) { wins++; } else if( score == 0 ) { draws++; } else if ( score == -1 ) { loses++; } } public static value O() { return value.O; } public static value X() { return value.X; } public value getPlayerSymbol() { return playerSymbol; } public void setPlayerSymbol(char symbol) { if( symbol == O ) { playerSymbol = value.O; } else if( symbol == X ) { playerSymbol = value.X; } } public void requestPlayerSymbol() { Scanner input = new Scanner(System.in); boolean reselect; int playerChoice; System.out.println("What symbol will player one use? (1 = O, 2 = X)"); do { reselect = false; try { playerChoice = input.nextInt(); if( playerChoice == 1 ) { playerSymbol = value.O; } else if ( playerChoice == 2 ) { playerSymbol = value.X; } else { System.out.println("Invalid selection, please select again"); reselect = true; } } catch (InputMismatchException inputMismatchException) { System.err.println("Please input an integer (1 = O, 2 = X)"); input.nextLine(); reselect = true; } } while( reselect == true ); } }
error is in the switch statement for addValue, it wants an enum values[] of the board class enum values[] of the player.Java Code:package naughtsAndCrosses; public class Board { private enum value { O, X, EMPTY }; private value[][] board = new value[3][3]; public int addValue(int row, int column, value val) { if( board[row][column] == value.EMPTY) { board[row][column] = val; return 1; } else { return -1; } } public void boardReset() { for( int row = 0 ; row < board.length ; row++ ) { for( int column = 0 ; column < board[row].length ; column++) { board[row][column] = value.EMPTY; } } } public int checkGameWon() { int complete = 0; // XXX // --- // --- if( (board[0][0] == value.O && board[0][1] == value.O && board[0][2] == value.O) || (board[0][0] == value.X && board[0][1] == value.X && board[0][2] == value.X) ) { complete = 1; } // --- // XXX // --- if( (board[1][0] == value.O && board[1][1] == value.O && board[1][2] == value.O) || (board[1][0] == value.X && board[1][1] == value.X && board[1][2] == value.X) ) { complete = 1; } // --- // --- // XXX if( (board[2][0] == value.O && board[2][1] == value.O && board[2][2] == value.O) || (board[2][0] == value.X && board[2][1] == value.X && board[2][2] == value.X) ) { complete = 1; } // X-- // X-- // X-- if( (board[0][0] == value.O && board[1][0] == value.O && board[2][0] == value.O) || (board[0][0] == value.X && board[1][0] == value.X && board[2][0] == value.X) ) { complete = 1; } // -X- // -X- // -X- if( (board[0][1] == value.O && board[1][1] == value.O && board[2][1] == value.O) || (board[0][1] == value.X && board[1][1] == value.X && board[2][1] == value.X) ) { complete = 1; } // --X // --X // --X if( (board[0][2] == value.O && board[1][2] == value.O && board[2][2] == value.O) || (board[0][2] == value.X && board[1][2] == value.X && board[2][2] == value.X) ) { complete = 1; } // X-- // -X- // --X if( (board[0][0] == value.O && board[1][1] == value.O && board[2][2] == value.O) || (board[0][0] == value.X && board[1][1] == value.X && board[2][2] == value.X) ) { complete = 1; } // --X // -X- // X-- if( (board[0][2] == value.O && board[1][1] == value.O && board[2][0] == value.O) || (board[0][2] == value.X && board[1][1] == value.X && board[2][0] == value.X) ) { complete = 1; } // XXX // XXX // XXX if( board[0][0] != value.EMPTY && board[0][1] != value.EMPTY && board[0][2] != value.EMPTY && board[1][0] != value.EMPTY && board[1][1] != value.EMPTY && board[1][2] != value.EMPTY && board[2][0] != value.EMPTY && board[2][1] != value.EMPTY && board[2][2] != value.EMPTY) { complete = -1; } return complete; } public String toString() { String string = ""; for( int row = 0 ; row < board.length ; row++ ) { for( int column = 0 ; column < board[row].length ; column++) { System.out.print(String.format("%-5s", board[row][column]) + " "); } System.out.println(); } return string; } }Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-23-2012, 04:23 AM #8
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
You probably need to read up on the MVC pattern.
- 05-23-2012, 10:14 PM #9
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
I will, but what comes to mind is the player should use the board's attribute since its enum is a subset of the board... but then a player doesnt have a board, I had envisioned the player and board to talk to each other through the naughtsAndCrosses class because that seemed logical as naughtsAndCrosses represent the complete system that is needed for a game of naughts and crosses.
If I used the player's enum of O and X then the board's enum would be redundant... so I should destroy that I think and have EMPTY as a stand-alone variable.
The problem here though is that the board isnt fit for purpose now, because its no longer a N&c board, its just a board that displays whatever values the player has. It would seem like the board is less defined this way
I could just say add takes in a player object and if player = X then board = X.
I think I'll go with that, maybe I'll change my mind after I ask google what java MVC pattern isLegend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-23-2012, 10:39 PM #10
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
I fixed the enum issue with if statements.
I have another issue (next time I'll define the classes before I write code!)
player 2's symbol is never set, it has a null value. so what I'm doing to set it isnt right. what am I doing wrong?
if( player1.getPlayerSymbol() == Player.O() )
I thought this made sense... but apparently not.
Player.O() is a static variable I use to reference the enum values.
apparently setting player1's symbol to O does not equal the O of the class.
since this is my first real program, with multiple classes could you guys be nice to me and help me sort out this issue.Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-28-2012, 05:39 PM #11
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
Turns out I had forgotten the ' ' to specify its a char type otherwise single characters are treated as strings --- now I'm working on the A.I and I have a communication problem I cant resolve
please help me sort this logic out.
basically I have a new class called a.i, it extends player... now I have modified the turn method to check for an instance of AI and then I want to pass the board.values[][] data to the AI class so it can analyses and make its move
how do I do this? values[][] is a enum type belonging to board. how can I tell the method in AI to take in values[][] as an argument when it has no idea what it is?
NaughtsAndCrosses class:
Java Code:package naughtsAndCrosses; import java.util.InputMismatchException; import java.util.Scanner; import chapter_x.TwoDimentionalShape_10_9; public class NaughtsAndCrosses { Scanner input = new Scanner(System.in); Scanner inputLine = new Scanner(System.in); Board board = new Board(); Player player1 = new Player(); Player player2 = new Player(); public void turn(Player player) { if( player instanceof AI ) { AI ai = (AI) player; board.addValue(ai.move(/*what goes in here?*/)); } int playerSelection; int legal; System.out.println(player.getPlayerSymbol() + " Please select a location"); do { legal = 0; try { playerSelection = input.nextInt(); switch(playerSelection) { case 1: legal = board.addValue(0,0, player); break; case 2: legal = board.addValue(0,1, player); break; case 3: legal = board.addValue(0,2, player); break; case 4: legal = board.addValue(1,0, player); break; case 5: legal = board.addValue(1,1, player); break; case 6: legal = board.addValue(1,2, player); break; case 7: legal = board.addValue(2,0, player); break; case 8: legal = board.addValue(2,1, player); break; case 9: legal = board.addValue(2,2, player); break; default: { System.out.println("Invalid option, please select 1-9"); } } if( legal == -1 ) { System.out.println("Invalid move, already taken. Please select" + " again (1-9)"); } } catch(InputMismatchException inputMismatchException) { System.err.println("Invalid input, please select (1-9)"); input.nextLine(); } } while (legal != 1); } public void postGame(int winner) { System.out.println("The game is over!"); if( winner != -1) //there is a winner { System.out.println("The winner is player " + winner); if( winner == 1 ) //player1 wins { player1.addScore(1); player2.addScore(-1); } else //victory == 2 player2 wins { player2.addScore(1); player1.addScore(-1); } } else //victory == -1 draw { System.out.println("Its a draw"); player1.addScore(0); player2.addScore(0); } } public boolean playAgain() { String playerSelection; boolean playAgain = false; boolean reselect; System.out.println("Do you want to play again? (y/n)"); do { try { reselect = false; playerSelection = inputLine.nextLine(); if( playerSelection.equals("y") || playerSelection.equals("Y") || playerSelection.equals("yes") || playerSelection.equals("Yes") || playerSelection.equals("YES") ) { playAgain = true; } else if( playerSelection.equals("n") || playerSelection.equals("N") || playerSelection.equals("no") || playerSelection.equals("No") || playerSelection.equals("NO") ) { System.out.println("Have a nice day"); } else { System.out.println("Invalid option, please select y/n"); reselect = true; } } catch(InputMismatchException inputMismatchException) { System.err.println("Invalid input, please select y/n"); reselect = true; } } while ( reselect == true ); return playAgain; } public int play() { int winner = 0; int gameType = selectGameType(); AI ai = new AI(); board.boardReset(); System.out.println(board.toString()); player1.requestPlayerSymbol(); if( gameType == 1 ) { if( player1.getPlayerSymbol() == Player.O() ) { ai.setPlayerSymbol('X'); } else { ai.setPlayerSymbol('O'); } } else if( gameType == 2) { if( player1.getPlayerSymbol() == Player.O() ) { player2.setPlayerSymbol('X'); } else { player2.setPlayerSymbol('O'); } } while (true) { turn(player1); System.out.println(board); if( board.checkGameWon() == 1 ) { winner = 1; break; } if( board.checkGameWon() == -1 ) { winner = -1; break; } if( gameType == 1 ) { turn(ai); } else if( gameType == 2) { turn(player2); } System.out.println(board); if( board.checkGameWon() == 1 ) { winner = 2; break; } if( board.checkGameWon() == -1 ) { winner = -1; break; } } return winner; } public int selectGameType() { int selection = 0; int playerSelection; boolean reselect; System.out.println("1: Single player 2: Two players"); do { try { reselect = false; playerSelection = input.nextInt(); if( playerSelection == 1 ) { selection = 1; } else if( playerSelection == 2 ) { selection = 2; } else { System.out.println("Invalid option please select 1 or 2"); reselect = true; } } catch( Exception e ) { System.out.println("Invalid input please select 1 or 2"); reselect = true; } } while( reselect == true ); return selection; } public static void main(String[] args) { NaughtsAndCrosses game = new NaughtsAndCrosses(); int winner; do { winner = game.play(); game.postGame(winner); } while(game.playAgain() == true); } }Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-28-2012, 05:43 PM #12
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
Sorry board.getValues() goes there I ment in the AI class, the parameters for that method
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-28-2012, 05:50 PM #13
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
Can you make a SMALL simple, complete program that should compile (when the errors are fixed) to show your problem?
If you don't understand my response, don't ignore it, ask a question.
- 05-28-2012, 11:32 PM #14
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
the pre-AI build works fine...
is it impossible to pass an enum into another class's method?
! got it, I'm such an idiot, I just need to pass the board object into the method... then manipulate its values[][] property that should work
one more thing, should I overload the turn method? or use if(instanceof ) elseLegend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-29-2012, 12:05 AM #15
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
This is a mess, but it might just work if I've doen this correctly, can you tell me whether this is correct for a AI rule
assume the ai to be O hereJava Code:// EOO // --- // --- if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[0][1] == Board.O() && board.getBoard()[0][2] == Board.O() ) { board.addValue(0,0, this); }
I'm going to have to go through each case where victory can be achieved, failing that I need to make another set of second priority rules for blocking two enamy moves in a row, then a third priority rule for if there is one O and emptys elsewhere and then the final priority is just a random generator
I thought of this but I dont know if it will work... is that code referencing the board values properly? I cant tell because eclipse lets me change the [][] and doesnt report any errors so I dont know...Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-29-2012, 01:12 AM #16
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Tic-Tac-Toe Version.1.0. Requesting feedback and help.
No body is that interested in this...
but for what its worth I got it to work! its the most amazing thing when you see your first a.i at work; its like you created something real.
I've only had time to do the first priority rule though, so much work doing these one by one... if no case exits I got the (do... while (!legal)) random moveLegend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-29-2012, 04:36 PM #17
Similar Threads
-
Requesting error help for my game.
By ruben381 in forum New To JavaReplies: 5Last Post: 04-27-2012, 04:07 AM -
Requesting Code Review
By Zeramat in forum New To JavaReplies: 9Last Post: 06-16-2011, 05:59 AM -
Requesting help on tweeking a JCheckBox JList
By benjamin.b in forum New To JavaReplies: 4Last Post: 06-07-2011, 02:45 PM -
Requesting a review of my LoginBox!
By aadem in forum New To JavaReplies: 4Last Post: 03-16-2011, 09:01 PM -
java -version pointing to older version
By deepakts in forum New To JavaReplies: 4Last Post: 05-06-2010, 09:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks