IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Assignment on TicTacToe

3 posters

Go down

Assignment on TicTacToe  Empty Assignment on TicTacToe

Post by BIT0322-Hira Sun Sep 25, 2011 4:44 pm

I have tried my own way to complete the assignment.
1.I take a [3][3] array.
2.I fixed two markers 'X' & 'O' for player 1 & 2.
3.Then I ask user to input & show him the game board.Which spot of the game board he selects,first I check that number is it in the game board? If it is there,I place his marker on that position otherwise ask him to input another number.Here is a problem.Because I ask the user for another input but in the meantime the playing option switched to next player.So I have to use switchPlayer() for two times if condition falls.
4.If a player fixes his spot the marker will be replaced with the position number.So if a player tries to
choose the spot again It will not find his desired position number because it is already replaced by a marker.So he can't input.
5.GameWinner() will check if there is same marker in a row,column or diagonal. i do this worker manually.I want an idea how can I do it more efficiently?
6.3,4 & 5 runs for 9 valid play,if it finds a winner breaks.otherwise after 9 valid plays it shows draw.

Do you have any suggestion to make the code more efficient?

Code:

public class tMain {
    public static void main(String[] args) {
        ticTacToe game = new ticTacToe();
        int continuePlaying=1;
       
        while (continuePlaying==1) {

            game.board();
            game.drawBoard();

            int player;
            int t=0;
            while ( game.getPlays() < 9) {
               
                int pick = 1;
                while (pick!=0) {
                    if (game.getCurrentPlayer() == 1)
                        player = game.getPlayer1();
                    else
                        player = game.getPlayer2();
       
                    System.out.print("It is Player" + player+ "'s turn. Pick a spot: ");
                    String square = game.Input();
                    if (square.length()==1&& Character.isDigit(square.toCharArray()[0])) {
                        pick = Integer.parseInt(square);
                       
                        pick = game.placeMarker(pick);
                       
                    }
                    if (pick == 0) {
                        System.out.println("Spot can not be selected. Retry");
                        game.switchPlayers();
                    }
                   
                   
                    game.drawBoard();
                    game.switchPlayers();
                    t=game.winner();
                    if(t==1)
                        break;
                }
                if(t==1)
                    break;
            }
            t=game.winner();
            if (t==1) {

                player = game.getCurrentPlayer();
                if (player == 1)
                    System.out.println("Game Over: **Player2** WINS!");
                if (player == 2)
                    System.out.println("Game Over: **Player1** WINS!");
            } else {
                System.out.println("Game Over: Draw");
            }
            System.out.println();
            System.out.print("Play again? (Y/N): ");
            String choice = game.Input();
            if (choice=="N") {
                continuePlaying=2;
            }
        }

    }
}

import java.util.*;

    public class ticTacToe{
        char[][] board = new char[3][3];
        int player1;
        int player2;
        int currentPlayer=1;
     
        int plays=0;
     
        char marker1 ='X';
       
        char marker2='O';

        protected void board() {
            int k=0;
            for (int i = 0; i < 3; i++) {
                for (int j= 0; j<3; j++) {
                    board[i][j] =Character.forDigit(++k,10);
                }
            }
            currentPlayer=1;
           
            plays=0;
        }
       
        void drawBoard() {
            System.out.println("Game board:");
            for (int i = 0; i < 3; i++) {
                for (int j= 0;j< 3;j++) {
                    System.out.print("[" +board[i][j] + "]");
                }
            System.out.println(); 
            }
         
        }
        void switchPlayers() {
            if (getCurrentPlayer()==1) {
                setCurrentPlayer(2);
            } else {
                setCurrentPlayer(1);
            }
          setPlays(getPlays()+1);
        }

        String Input() {
            String input="";
            Scanner sc=new Scanner(System.in);
            input=sc.next();
            return input;
        }

        int placeMarker(int play) {
            for (int i = 0; i < 3; i++) {
                for (int j= 0; j< 3; j++) {
                    if (board[i][j]==Character.forDigit(play,10)) {
                        if(getCurrentPlayer()==1)
                            board[i][j]=marker1;
                        else
                            board[i][j]=marker2;
                        return (play+1);
                    }
                }
            }
            return 0;
        }

        int  winner() {
         
            char current = ' ';
         
                    current =board[0][0];
                    if (Character.isLetter(current) && board[0][1] == current && board[0][2] == current) {
                        return 1;
                    }
                    current =board[1][0];
                    if
                    (Character.isLetter(current) && board[1][1] == current && board[1][2] == current) {
                        return 1;
                    }
                   
                    current =board[2][0];
                    if (Character.isLetter(current) && board[2][1] == current && board[2][2] == current) {
                        return 1;
                    }
                   
                   
                   
                    current =board[0][0];
                    if (Character.isLetter(current) && board[1][0] == current && board[2][0] == current) {
                        return 1;
                    }
                   
                   
                    current =board[0][1];
                    if (Character.isLetter(current) && board[1][1] == current && board[2][1] == current) {
                        return 1;
                    }
                    current =board[0][2];
                    if (Character.isLetter(current) && board[1][2] == current && board[2][2] == current) {
                        return 1;
                    }
                   
                   
                   
                    current =board[0][0];
                    if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current) {
                        return 1;
                    }
                    current =board[0][2];
                    if (Character.isLetter(current) && board[1][1] == current && board[2][0] == current) {
                        return 1;
                    }
                    else
                        return 0;
              }
         
       
     

       

     

        int getCurrentPlayer() {
            return currentPlayer;
        }

        void setCurrentPlayer(int currentPlayer) {
            this.currentPlayer = currentPlayer;
        }

        int getPlays() {
            return plays;
        }

        void setPlays(int plays) {
            this.plays = plays;
        }

        int getPlayer1() {
            int player1=1;
            return player1;
        }

        void setPlayer1(int player1) {
            this.player1 = player1;
        }

        int getPlayer2() {
            int player2=2;
            return player2;
        }

        void setPlayer2(int player2) {
            this.player2 = player2;
        }
    }



BIT0322-Hira
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : O+
Posts : 24
Points : 33

Back to top Go down

Assignment on TicTacToe  Empty Re: Assignment on TicTacToe

Post by BIT0112-Rokon Wed Sep 28, 2011 4:42 am

No doubt it’s a good work. One think just immediately come to my mind that you didn’t follow coding convention. I didn’t check your logic. The problem I have found while my first scans are -

Change your class name. By convention and coding standard, class
name should start with uppercase. And consequent words are also should be uppercase. Example - TicTacToe
2. Class variable should be private.
3. The methods those are not visible outside the class, should be private.
4. String Input(), method is not understandable. same things true for the for winner(). You could write it getWinner().
5. What does the method board() do? I didn't get it. so, change it.


Suggestion:
1. To make code more readable, follow coding convention strictly.
2. Method name should be a verb and variable, and class name should be a noun.
3. study on java coding standard and coding convention .


I didn't check your logic. I will do it whenever I get some free time.
All the best and happy coding. [You must be registered and logged in to see this image.]
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Assignment on TicTacToe  Empty Re: Assignment on TicTacToe

Post by BIT0112-Rokon Wed Sep 28, 2011 4:59 am

Good
work, now do it again in swing. User doesn't like to play a game in the console. So make a good user interface with swing. Study a bit on Java's swing. I think you will enjoy it. Take a week; I will knock you after a week to see your progress. All the best,
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Assignment on TicTacToe  Empty Re: Assignment on TicTacToe

Post by BIT0122-Amit Wed Sep 28, 2011 5:59 am

Just in case you don't know what is Swing, it is something that can be used to create GUI using Java Smile

There are many tutorials available.
One of my favorites is this one-

[You must be registered and logged in to see this link.]
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Assignment on TicTacToe  Empty Re: Assignment on TicTacToe

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum