6 Replies - 1189 Views - Last Post: 09 February 2013 - 08:28 PM Rate Topic: -----

#1 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

what do you guys think of my tic-tac-toe game?

Posted 09 February 2013 - 06:49 PM

I'm in the middle of learning Java right now and a project peaked my interests, so I attempted to do it. The project was a tic-tac-toe AI, but I'm not quite there so I created a tic-tac-toe game where the bot plays randomly, making it very easy :)

I was wondering how I did, what you would have done differently, and what I could improve on.

There are 4 files:


tictactoe.java: The main file that handles inputs, starts games, and so on
Spoiler


Game.java: Controls the game, checks for wins, makes the bot play, etc
Spoiler


Board.java: Used for storing the position of plays
Spoiler


AI.java: The bot. It accepts a Board object, finds an empty spot, and places an X there and returns the board.
Spoiler




Thanks you for your time

This post has been edited by creativecoding: 09 February 2013 - 06:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: what do you guys think of my tic-tac-toe game?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: what do you guys think of my tic-tac-toe game?

Posted 09 February 2013 - 06:57 PM

*
POPULAR

Your code is well structured... but I don't see why you use static variable/method in your tictactoe class
This means you missed a few OO concepts.. the only static methods/variable should me main()

Also respect Java conventions...class names start by a capital letter so Tictactoe or TicTacToe but not tictactoe
Was This Post Helpful? 5
  • +
  • -

#3 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: what do you guys think of my tic-tac-toe game?

Posted 09 February 2013 - 07:17 PM

Thanks!

I fixed the naming conventions.

About the static methods/variables though, I'm unable to call non-static stuff from inside a static method, so should I be putting all of my input stuff into it's own class and running that from main()?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7756
  • View blog
  • Posts: 13,116
  • Joined: 19-March 11

Re: what do you guys think of my tic-tac-toe game?

Posted 09 February 2013 - 07:25 PM

There are some details that you could pay attention to.

	    public void putChar(int y, int x, char Char){
	        try {
	            board[y][x] = Char;
	        } catch (Exception ex){
	            System.out.println("There was a problem!");
	            ex.printStackTrace();
	        }


What exception do you expect here? Name it and deal with it. Or, since the only thing you could be catching here is an ArrayOopsieException, just do your bounds checking.
But the important thing is: Exceptions should be handled by name, not by the generic Exception class. Catching Exception e defeats the whole purpose of Java's exception model, which is to facilitate communication between pieces of code.

Also, please observe naming conventions. Char, with capital C, suggests a class, this is a char variable. More importantly, your names should reflect what you're actually doing. You're not putting a char, you're registering a move on the board. The fact that you're using chars to represent this (instead of ints, or Strings, or anything else) is completely uninteresting: what's interesting is that this is how the board learns that a move has been made.


Other similar issues could be raised.

On a larger, structural level, I would suggest a slightly different organization. Whoever is responsible for running the game should know about two Player objects: a Player is simply a source of moves. The Player interface is pretty simple. It has one method, nextMove()* which takes an argument representing either the current situation of the game or the most recent change to that situation (this is an implementation decision) and returning a move - however you represent that move.

It should be obvious that this can be either a human player or an AI, and more than that, if it's a human player it can talk to a GUI or a CLI or any other mode of communication, and if it's an AI player it's easy to plug in any old AI you write: you could easily allow the player to choose among many electrical players.


*Other names could be reasonable


Also - welcome back, pbl! Hope you enjoyed your vacation!
Was This Post Helpful? 3
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: what do you guys think of my tic-tac-toe game?

Posted 09 February 2013 - 07:27 PM

Wrong design :)
The main() is just there to instantiate a object of the class it belonmgs to....
and may be to create the Scanner for user interface that should be pass to the created object

class TicTacToe {

    // constructor
    TicTacToe() {
      ...
    }

    void userPlay(char usrMoce) {
      ....
    }

    public static void main(String[] arg) {
      Scanner scan = new Scanner(System.in);
      TicTacToe ttt = new TicTacToe);
      while(true) {
        char c = scan.nextLine(),charAt(0);
        ttt.userPlay(c);


Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,141
  • Joined: 27-December 08

Re: what do you guys think of my tic-tac-toe game?

Posted 09 February 2013 - 08:19 PM

@creativecoding: Regarding your getInput() method, it's expensive to keep creating BufferedInput objects. Create only one outside of your method and use it in the method.
Was This Post Helpful? 2
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: what do you guys think of my tic-tac-toe game?

Posted 09 February 2013 - 08:28 PM

When you establish a phone call with a friend do you hang up and recall at each sentence ?

Create only one channel assigned to System.in and used it for all your console input
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1