Results 1 to 2 of 2
  1. #1
    Wazniak is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Java simple game template

    Hey! I'm trying to learn java again and doing some exercises, I am struggling with understanding this task:
    Write a code that handles tic-tac-toe game on a square board. I have a template like this:
    Java Code:
    public class Test4{
    
    public void setBoardSize(int size) {   }
    
       public void setO(int a, int b) {   } 
    
       public void setX(int a, int b) {   }
    
       public boolean hasEmpty() {   }
    
       public String toString() {   }
    
    }

    my code:
    Java Code:
    class Test4 {
       int a,b,size;
       // int[][] tab = new int[size][size];
    
       public void setBoardSize(int size) {
    	   
    		this.size = size;
                   /* clearing board / setting to 0 I guess I have to do it one way or another ?
                   for(int i =0;i<size.length;i++){
                    for(int j =0;i<size.length;j++)     
                      size[i][j] = 0;
                    }
                   */
       }
    
       public void setO(int a, int b) {
    	
    	if(size >0){  
    		this.a = a;
    		this.b = b;
            /* smth alongside this ?
            tab[this.a][this.b]=tab[a][b];
          */
       }
       } 
    
       public void setX(int x, int y) {
    	
    	if(size > 0){
    		this.x = x;
    		this.y = y; 
             // like above
       }
       }
    
             // i thought about generating this "+-" template (see rules below) using smth like this:
    	// String str = new String(new char[size]).replace("\0", "+-");
    	// String str2 = new String(new char[size]).replace("\0", "+\n|");
      
     public String toString() {
    	   
    	if(size < 1) return null;	
       }
    
       public boolean anyEmpty() {
    	if (size < 1) return false;
       }
    }
    rules: method setBoardSize should be called 1st, before calling setBoardSize, setO and setX should be ignored, calling toString() should return null, hasEmpty return false.

    method setO - should set circle on board
    method setX - should set X on board
    method hasEmpty - return true untill board is full
    method toString should generate actual board state and output smth like this (after calling System.out.println(reference to Test4)):
    +-+-+-+
    |O |X| |
    +-+-+-+
    | |X| |
    +-+-+-+
    | |O| |
    +-+-+-+

    program should communicate and ignore about wrong moves:
    - 2 and more same moves (eg. Twice X in row)
    - placing X/O outside the board
    - placing X/O on a spot already taken
    Starting position is (0,0) - upper left corner on board.
    I do not need main method.
    Thanks in advance for any help :)!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Java simple game template

    Did you have any questions about the program?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Simple Mastermind Java Game, Please help
    By williamsb14 in forum New To Java
    Replies: 3
    Last Post: 11-24-2012, 09:52 PM
  2. Simple Java Game -- HELP :)
    By Gamersunited22 in forum New To Java
    Replies: 7
    Last Post: 04-04-2012, 07:41 PM
  3. want to make a simple game in java
    By Tarasj in forum New To Java
    Replies: 1
    Last Post: 03-26-2012, 10:54 PM
  4. need a simple java game
    By Shashwat in forum New To Java
    Replies: 1
    Last Post: 01-03-2011, 03:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •