Results 1 to 1 of 1
Thread: Tic Tac Toe game
- 10-24-2011, 04:34 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
Tic Tac Toe game
Hello
please help me to fix my code, try it in ur devices to see the result:
I am looking for your replyJava Code:package Tictactoe; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author ituser */ import java.util.*; public class Tictactoe1 { private static final int ROWS = 3; private static final int COLUMNS = 3; private static String[][] board; //construct an empty board public Tictactoe1() { board = new String[ROWS][COLUMNS]; //Fill with spaces for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { board[i][j] = " "; } } } //set the fields in the board public String set(int i, int j, String player) { if (board[i][j].equals(" ")) { board[i][j] = player; } else { return " "; } return board[i][j]; } //set the fields in the board public boolean checkDiagL(String player) { boolean found = false; int count = 0; int i = 0; int j = 0; while (i < ROWS) { if (board[i][j].equals(player)) { count++; i++; j++; } else { break; } } if (count == ROWS) { found = true; } return found; } public boolean checkDiagR(String player) { boolean found = false; int count = 0; int i = 0; int j = COLUMNS - 1; while (i < ROWS) { if (board[i][j].equals(player)) { count++; i++; j--; } else { break; } } if (count == ROWS) { found = true; } return found; } public boolean checkHorizontal(String player) { boolean found = false; int count = 0; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { if (board[i][j].equals(player)) { count++; } else { break; } } if (count == ROWS) { found = true; break; } else { count = 0; } } return found; } public boolean checkVertical(String player) { boolean found = false; int count = 0; for (int j = 0; j < COLUMNS; j++) { for (int i = 0; i < ROWS; i++) { if (board[i][j].equals(player)) { count++; } else { break; } } if (count == COLUMNS) { found = true; break; } else { count = 0; } } return found; } public static String preventdiagonalsLeft() { String player = "x"; boolean found = false; int i = 0; int j = 0; int count = 0; String block = ""; while (i < ROWS) { if (board[i][j] == player) { count++; } else if (board[i][j] == " ") { block = "Block: " + i + " " + j; } i++; j++; } if (count == 2) { found = true; return block; } return ""; } public static String preventdiagonalsRight() { String player = "x"; boolean found = false; int i = 0; int j = 2; int count = 0; String block = ""; while (i < ROWS) { if (board[i][j] == player) { count++; } else if (board[i][j] == " ") { block = "Block: " + i + " " + j; } i++; j--; } if (count == 2) { found = true; return block; } return ""; } public static String preventhorizontal() { String player = "x"; boolean found = false; int count = 0; String block = ""; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { if (board[i][j] == player) { count++; } else if (board[i][j] == " ") { block = "Block: " + i + " " + j; } } if (count == 2) { found = true; return block; } else { count = 0; } } return ""; } public static String preventvertical() { String player = "x"; boolean found = false; int count = 0; String block = ""; for (int j = 0; j < COLUMNS; j++) { for (int i = 0; i < ROWS; i++) { if (board[i][j] == player) { count++; } else if (board[i][j] == " ") { block = "Block: " + i + " " + j; } } if (count == 2) { found = true; return block; } else { count = 0; } } return ""; } public static int[] nextMove(String player) { int counter = 0; int pos[] = new int[18]; for (int i = 0; i < 18; i++) { pos[i] = -1; } /* lucky position in the center of board*/ if (board[1][1] == " ") { return new int[]{1, 1}; } /* choose available move */ for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == " ") { pos[counter] = i; } pos[counter + 1] = j; counter = counter + 2; } } return pos; } public boolean checkWinner(String player) { if (checkDiagL(player)) { return true; } else if (checkDiagR(player)) { return true; } else if (checkHorizontal(player)) { return true; } else if (checkVertical(player)) { return true; } else { return false; } } public static String preventAll() { String block = preventdiagonalsLeft(); if (block.equals("")) { block = preventdiagonalsRight(); if (block.equals("")) { block = preventhorizontal(); if (block.equals("")) { block = preventvertical(); } } } return block; } /**creates a string representation of the board * @return the string representation */ public String toString() { String r = ""; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { r += board[i][j]; if (j < COLUMNS - 1) { r += "|"; } } r += "\n"; if (i < ROWS - 1) { for (int j = 0; j < (COLUMNS * 2 - 1); j++) { r += "-"; } r += "\n"; } } return r; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String player = "x"; Tictactoe1 game = new Tictactoe1(); boolean done = false; System.out.println(game.toString()); while (!done) { int row = 0, column = 0; if (player.equals("o")) { nextMove("o"); preventAll(); } else { System.out.print("Row for " + player + " (-1 to exit): "); row = in.nextInt(); if (row < 0) { done = true; break; } else { System.out.print("Column for " + player + ": "); column = in.nextInt(); } } if (game.set(row, column, player).equals(player)) { System.out.println(game.toString()); if (game.checkWinner(player)) { System.out.println("Result: The Winner is " + player); break; } if (player.equals("x")) { player = "o"; } else { player = "x"; } } else { System.out.println("Position selected!"); } } } }
Similar Threads
-
Complete Game Engine for beginner and intermediate game programmers
By rdjava in forum Java GamingReplies: 1Last Post: 06-02-2011, 09:29 AM -
Hi Lo Game
By Bgreen7887 in forum New To JavaReplies: 5Last Post: 10-22-2010, 03:08 AM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06-10-2010, 08:06 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks