Jump to content

Douglas Henderson

Members
  • Posts

    6
  • Joined

  • Last visited

Douglas Henderson's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. Quick question, where did you get the number 9 from and 'incomplete'? Also should I also put in 'EMPTY' in with the code...example.. if(gameboard[0][0] + gameboard[0][1] + gameboard[0][2] == 3*EMPTY){return EMPTY;} if(gameboard[1][0] + gameboard[1][1] + gameboard[1][2] == 3*EMPTY){return EMPTY;} if(gameboard[2][0] + gameboard[2][1] + gameboard[2][2] == 3*EMPTY){return EMPTY;} Thank you
  2. Thank you, so the if/else structure in the below main method is the way to go? displayBoard(); playerVal = (turn % 2 == 0)? NOUGHT : CROSS; if (playerVal == NOUGHT) { System.out.println ("\n-0's turn-"); } else { System.out.println("\n-X's turn-"); } System.out.print("Enter row and Column:"); try { set(playerVal, scan.nextInt()); } catch (IllegalArgumentException ex) {System.err.println(ex);} turn ++; outcome = winOrTie(); } while ( outcome == -2 ); displayBoard();
  3. This is a small assignment I have been given. They have given me most of the code and I have to figure out the rest. My winorTie method is what I have to figure out. So yes I am pretty much teaching myself. Modified it a bit adding brackets. Do I still have too many semicolons? I am trying to figure out what code to put in though. It is a work in progress. Part of the directions... The variable gameboard is a two-dimensional int array. Think of it as a table with rows and columns, and cells can be empty (0) or contain a nought (–1) or cross (1). The constants EMPTY, NOUGHT, and CROSS will simplify your code. Define the winOrTie method. Check first for a win with rows and columns and then diagonally. Finally, check to see if there are any empty cells without a cross or nought. static int winOrTie() { if ( ( ) ) { return CROSS; } else if ( ( ) ) { return NOUGHT; } else if ( ( ) ) { return } else { return } }
  4. Hi, I am in need of help with this tic tac toe game. I have been working on it longer than I care to admit. I am trying to get it to compile correctly. I am working on the winorTie method and been having trouble figure out what code to insert. I understand that I need to write code that goes through the game board row by row and counts how many X's or O's have been marked. Yet every time I've inserted code it doesn't compile correctly. Right now I have the foundation for the if statement but unsure about the code. Whenever I compile I get the below in the output section. | | | | ------ | | | | ------ | | | | ------ -0's turn- When compiled correctly it is supposed to look like this in the output section. |||| ------- |||| ------- |||| ------- --O’s turn-- |O| | | ------- |||| ------- |||| ------- --X’s turn-- |O|X| | ------- |||| ------- |||| ------- --O’s turn-- |O|X| | ------- | |O| | ------- |||| ------- --X’s turn-- |O|X| | ------- | |O| | ------- |X| | | ______ —O’s turn— |O|X| | ------- | |O| | ------- |X| |O| O wins! Here is my current code.. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tictactoegame; /** * * @author Doug Henderson */ public class tictactoegame { static int [] [] gameboard; static final int EMPTY = 0; static final int NOUGHT = -1; static final int CROSS = 1; static void set (int val, int row) throws IllegalArgumentException { int col = 0; if (gameboard[row] [col] == EMPTY) gameboard[row] [col] = val; else throw new IllegalArgumentException("Player already there!"); } static void displayBoard () { for (int[] gameboard1 : gameboard) { System.out.print("|"); for (int c = 0; c < gameboard1.length; c++) { switch (gameboard1[c]) { case NOUGHT: System.out.print("0"); break; case CROSS: System.out.print("X"); break; default: //Empty System.out.print(" "); } System.out.print("|"); } System.out.println("\n------\n"); } } static void createBoard(int rows, int cols) { gameboard = new int [rows] [cols]; } static int winOrTie() { if (); return ; else if (); return ; else if ();{ return ; else { return ; } /** * @param args the command line arguments */ /** * @param args the command line arguments */ public static void main(String[] args) { createBoard(3,3); int turn = 0; int playerVal; int outcome; java.util.Scanner scan = new java.util.Scanner(System.in); do { displayBoard(); playerVal = (turn % 2 == 0)? NOUGHT : CROSS; if (playerVal == NOUGHT) { System.out.println ("\n-0's turn-"); } else { System.out.println("\n-X's turn-"); } System.out.print("Enter row and Column:"); try { set(playerVal, scan.nextInt()); } catch (IllegalArgumentException ex) {System.err.println(ex);} turn ++; outcome = winOrTie(); } while ( outcome == -2 ); displayBoard(); switch (outcome) { case NOUGHT: System.out.println("0 wins!"); break; case CROSS: System.out.println("X wins!"); break; case 0: System.out.println("Tie."); break; } } }
×
×
  • Create New...