Results 1 to 3 of 3
Thread: First TicTacToe Attempt
- 07-25-2010, 08:26 AM #1
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
First TicTacToe Attempt
Here is my first attempt at TicTacToe.
1) Any thoughts, comments, suggestions, or recommendations, on how I can improve this code?
2) Any thoughts on how I can add a computer player that does not just randomly pick a location. I have googled this, and think I am not understanding the logic behind it.
Kinda looks to me like it belongs on the dailyWTF. But it is "Student" code if you will.
Thanks in advance.
Java Code:package TicTacToe; import java.util.*; public class TicTacToe { public static void main(String[] args) { Scanner console = new Scanner(System.in); int[][] playField = new int[3][3]; int players = 0; //player 1 = true, player 2 = false; boolean player = false, gameWon = false; players = startGame(console); initilizeField(playField); do{ drawField(playField); player = !player; if (player == true && players == 1 || player == true && players == 2 || player == false && players == 2){ playerChoice(player, console, playField); }else if (player == false && players != 2){ //computerChoice(player, playField); System.out.print("This would be Player 2's Turn\n"); } gameWon = isGameWon(playField); } while(!gameWon); winningMessage(player); } //displays message at end of game to notify user game is over public static void winningMessage(boolean player){ int playerInt = (player == true) ? 1 : 2; System.out.print("You Won Player " + playerInt + "\n"); System.out.print("Thanks for Playing\n"); } //validates the game has not been won public static boolean isGameWon(int[][] playField){ if ( //Horizontal Rows top to bottom playField[0][0] == playField[0][1] && playField[0][0] == playField[0][2] && playField[0][0] != 0|| playField[1][0] == playField[1][1] && playField[1][0] == playField[1][2] && playField[1][0] != 0|| playField[2][0] == playField[2][1] && playField[2][0] == playField[2][2] && playField[2][0] != 0|| //Vertical Rows left to right playField[0][0] == playField[1][0] && playField[0][0] == playField[2][0] && playField[0][0] != 0|| playField[0][1] == playField[1][1] && playField[0][1] == playField[1][2] && playField[0][1] != 0|| playField[0][2] == playField[1][2] && playField[0][2] == playField[2][2] && playField[0][2] != 0|| //Diagonal Rows) playField[0][0] == playField[1][1] && playField[0][0] == playField[2][2] && playField[0][0] != 0|| playField[0][2] == playField[1][1] && playField[0][2] == playField[2][0] && playField[0][2] != 0){ return true; } return false; } //takes input from user, sets choice in playField[][] public static void playerChoice(boolean player, Scanner console, int[][] playField){ int choice = (player == true) ? 1 : 2; int location; boolean doLoop = true; do { System.out.print("Please Make Your Move player " + choice + "\n:"); location = console.nextInt(); doLoop = setChoice(location, choice, playField); if (doLoop == true) { System.out.print("That location is taken. Please Choose again.\n"); } } while (doLoop); } //takes input from user if one or two users. displays key map. public static int startGame(Scanner console){ int choice = 0; System.out.print("This is a simple Java TicTacToe Game\n"); System.out.print("Use the Number Pad to make your Selection\n"); System.out.print(" 7 | 8 | 9\n" + "---|---|---\n" + " 4 | 5 | 6\n" + "---|---|---\n" + " 1 | 2 | 3\n\n"); System.out.print("Please Choose One or Two Players\n:"); do{ choice = console.nextInt(); if (choice == 1 || choice == 2){ return choice; } System.out.print("Please enter 1 or 2\n:"); } while(true); } //initilizes all play spaces to 0 public static void initilizeField(int[][] playField){ for( int i= 0; i < 3; i++){ for ( int j = 0; j < 3; j++){ playField[i][j]= 0; } } } // Draws the Play Field public static void drawField (int[][] playField){ for( int i = 0; i < 3; i++){ for( int j = 0; j < 3; j++){ System.out.print(checkField(playField[i][j])); if ( j != 2){ System.out.print("|"); } else{ System.out.print("\n"); } } if( i != 2){ for( int j = 0; j < 3; j++){ System.out.print("---"); if ( j != 2){ System.out.print("|"); } } } System.out.print("\n"); } } // Checks if a blnk X or O should be played public static String checkField(int field){ switch (field){ case 0: return " "; case 1: return " X "; case 2: return " O "; } return null; } //validates no existing move in location. //changes the value of the playField array based on player choice public static boolean setChoice(int location, int choice, int[][] playField){ switch(location){ case 7: if (playField[0][0] == 0){ playField[0][0] = choice; return false; } return true; case 8: if (playField[0][1] == 0){ playField[0][1] = choice; return false; } return true; case 9: if (playField[0][2] == 0){ playField[0][2] = choice; return false; } return true; case 4: if (playField[1][0] == 0){ playField[1][0] = choice; return false; } return true; case 5: if (playField[1][1] == 0){ playField[1][1] = choice; return false; } return true; case 6: if (playField[1][2] == 0){ playField[1][2] = choice; return false; } return true; case 1: if (playField[2][0] == 0){ playField[2][0] = choice; return false; } return true; case 2: if (playField[2][1] == 0){ playField[2][1] = choice; return false; } return true; case 3: if (playField[2][2] == 0){ playField[2][2] = choice; return false; } return true; default: return false; } } }
- 07-25-2010, 01:01 PM #2
I wrote a Tic Tac Toe game in javascript when I was learning javascript. You could look at its logic to get some ideas for the computer's play.
TicTacToe using JavaScript
The basic idea was to build tables to hold combinations of 3 in a row and use that.
- 07-25-2010, 09:32 PM #3
Similar Threads
-
Switching JPanels inside JFrame attempt
By frenk_castle in forum AWT / SwingReplies: 7Last Post: 03-31-2010, 08:39 AM -
my Quicksort attempt has failed
By Jeremy8 in forum New To JavaReplies: 4Last Post: 11-16-2009, 02:56 AM -
HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted
By R O C K Y in forum XMLReplies: 0Last Post: 02-18-2009, 05:20 AM -
TicTacToe
By Joe3161 in forum New To JavaReplies: 4Last Post: 11-25-2007, 06:47 PM -
Security Violation:attempt to use Restricted Class: sun.jdbc.odbc.JdbcOdbcDriver
By Heather in forum Advanced JavaReplies: 1Last Post: 07-14-2007, 05:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks