Title: Pausable Tic Tac Toe
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Using Serializeable to make a Pausable Tic-Tac-Toe game

In this video, we will start out with the published answer to cps161 homework to create a Tic-Tac-Toe game. We will make the class Serializeable and then we will add a feature where we can Pause the game. When Tic-Tac-Toe starts up again after a Paused game, we will start back up from the same place where we were Paused. This feature will be done by writing a Serialized version of our game to a "Pause" file. When Tic-Tac-Toe is restarted we will initialize our class by reading in our class from the Pause file.

Original Tic-Tac-Toe

package ttt_original;

import java.util.*;

class TicTacToe
{
    char ttt[][] = new char[3][3];
    static final char player1 = 'O';
    static final char player2 = 'X';
    char currPlayer;
    Scanner scan  =new Scanner(System.in);

    String playerID(char player)
    {
        if (player == player1)
            return "player1: "+player;
        else
            return "player2: "+ player;
    }

    void getPlayerInput(char player)
    {
       int row, col, cellVal;
       char cell;
       String temp;
       boolean continueFlag=true;
       System.out.println("Entering input for: " + playerID(player));
       do
       {
           System.out.println("enter in cell  [a, b, ... i]");
           temp = scan.next();
           cell = temp.charAt(0);
           if (cell < 'a' || cell > 'i')
           {
                System.out.println(cell + " cell is out of bounds ... must be a-i");
           }
           else
           {
                cellVal = cell - 'a';
                row = cellVal/3;
                col = cellVal % 3;

                if (ttt[row][col] != ' ')
                {
                    System.out.println("Cell already taken ... try again");
                }
                else
                {
                    ttt[row][col]=player;
                    continueFlag = false ; // done now that we found a legal spot
                }
           }

        } while (continueFlag);
    }

    boolean gameIsDraw()
    {
        for(int i=0; i<3; i++)
        {
            for(int j=0; j<3; j++)
                if (ttt[i][j] == ' ')
                    return false;
         }
         return true;
    }

    boolean winner(char player)
    {
    // Check for a win on the rows
     boolean match;
     for(int i=0; i<3; i++)
        {
            match = true;
            for(int j=0; j<3; j++)
            {
                if (ttt[i][j] != player)
                {
                    match=false;
                    break;
                }
            }
            if (match)return true; // found a row owned by player
        }

      // Check for a win on the Columns
      for(int i=0; i<3; i++)
        {
            match=true;
            for(int j=0; j<3; j++)
            {
                if (ttt[j][i] != player)
                {
                    match=false;
                    break;
                }
            }
            if (match) return true; // found a col owned by player
        }

        // Check Diagonal from top left to bottom right
        match = true;
        for (int i=0; i < 3; i++)
            if (ttt[i][i] != player)
            {
                match = false;
                break;
            }
        if (match) return true; // Player owns diagonal


        // Check Diagonal from top right to bottom left
        match = true;
        for (int i=0; i < 3; i++)
            if (ttt[i][2-i] != player)
            {
                match = false;
                break;
            }
        if (match) return true; // Player owns the other diagonal

        return false; // Player hasn't won yet
    }

    void displayBoard()
    {
        System.out.println("********************************");
        System.out.println("      ---a------b------c---");

        for (int i=0; i<3; i++)
        {
            for (int j=0; j< 3; j++)
            {
              if (j == 0) System.out.print("      |  ");
              System.out.print(ttt[i][j]);
              if (j < 2) System.out.print( "   |  ");
              if (j==2)  System.out.print("  |");
            }
            System.out.println();
            switch (i)
            {
            case 0:
                System.out.println("      ---d------e------f---");
                break;
            case 1:
                System.out.println("      ---g------h------i---");
                break;
            case 2:
                System.out.println("      ---------------------");
                break;
            }
        }
    }


    void newgame()
    {
        currPlayer = player1;
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                ttt[i][j] =' ';

        boolean continueFlag = true;
        while (continueFlag)
        {
            displayBoard();
            if (gameIsDraw())
            {
                System.out.println("Game Ends in Draw");
                continueFlag = false;
            }
            else
            {
                getPlayerInput(currPlayer);
                if (winner(currPlayer))
                {
                    System.out.println("We have a winner: " + playerID(currPlayer));
                    displayBoard();
                    continueFlag = false;
                }
                else
                {
                    if (currPlayer == player1) currPlayer = player2;
                        else currPlayer = player1;
                }
             }
        }

    }


    public static void main(String[] args)
    {
        TicTacToe game = new TicTacToe();
        String str;
        do
        {
            game.newgame();

            System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
            str= game.scan.next();
        } while ("y".equals(str));

        System.out.println("Bye");
    }
}


Final Pausable Tic-Tac-Toe game (changes are highlighted)

package ttt_pausable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;

class PauseException extends RuntimeException
{

    private static final long serialVersionUID = 1L;

}


class TicTacToe implements Serializable
{
    private static final long serialVersionUID = 1L;

    char ttt[][] = new char[3][3];
    static final char player1 = 'O';
    static final char player2 = 'X';
    char currPlayer;
    static Scanner scan  =new Scanner(System.in);

    String playerID(char player)
    {
        if (player == player1)
            return "player1: "+player;
        else
            return "player2: "+ player;
    }

    void getPlayerInput(char player)
    {
       int row, col, cellVal;
       char cell;
       String temp;
       boolean continueFlag=true;
       System.out.println("Entering input for: " + playerID(player));
       do
       {
           System.out.println("enter in cell  [a, b, ... i]  or p for Pause");
           temp = scan.next();
           if ("p".equals(temp))
               throw new PauseException();

           cell = temp.charAt(0);
           if (cell < 'a' || cell > 'i')
           {
                System.out.println(cell + " cell is out of bounds ... must be a-i");
           }
           else
           {
                cellVal = cell - 'a';
                row = cellVal/3;
                col = cellVal % 3;

                if (ttt[row][col] != ' ')
                {
                    System.out.println("Cell already taken ... try again");
                }
                else
                {
                    ttt[row][col]=player;
                    continueFlag = false ; // done now that we found a legal spot
                }
           }

        } while (continueFlag);
    }

    boolean gameIsDraw()
    {
        for(int i=0; i<3; i++)
        {
            for(int j=0; j<3; j++)
                if (ttt[i][j] == ' ')
                    return false;
         }
         return true;
    }

    boolean winner(char player)
    {
    // Check for a win on the rows
     boolean match;
     for(int i=0; i<3; i++)
        {
            match = true;
            for(int j=0; j<3; j++)
            {
                if (ttt[i][j] != player)
                {
                    match=false;
                    break;
                }
            }
            if (match)return true; // found a row owned by player
        }

      // Check for a win on the Columns
      for(int i=0; i<3; i++)
        {
            match=true;
            for(int j=0; j<3; j++)
            {
                if (ttt[j][i] != player)
                {
                    match=false;
                    break;
                }
            }
            if (match) return true; // found a col owned by player
        }

        // Check Diagonal from top left to bottom right
        match = true;
        for (int i=0; i < 3; i++)
            if (ttt[i][i] != player)
            {
                match = false;
                break;
            }
        if (match) return true; // Player owns diagonal


        // Check Diagonal from top right to bottom left
        match = true;
        for (int i=0; i < 3; i++)
            if (ttt[i][2-i] != player)
            {
                match = false;
                break;
            }
        if (match) return true; // Player owns the other diagonal

        return false; // Player hasn't won yet
    }

    void displayBoard()
    {
        System.out.println("********************************");
        System.out.println("      ---a------b------c---");

        for (int i=0; i<3; i++)
        {
            for (int j=0; j< 3; j++)
            {
              if (j == 0) System.out.print("      |  ");
              System.out.print(ttt[i][j]);
              if (j < 2) System.out.print( "   |  ");
              if (j==2)  System.out.print("  |");
            }
            System.out.println();
            switch (i)
            {
            case 0:
                System.out.println("      ---d------e------f---");
                break;
            case 1:
                System.out.println("      ---g------h------i---");
                break;
            case 2:
                System.out.println("      ---------------------");
                break;
            }
        }
    }


    void newgame(boolean restart)
    {
        if (restart == false)
        {
            currPlayer = player1;
            for(int i=0; i<3; i++)
                for(int j=0; j<3; j++)
                    ttt[i][j] =' ';
        }

        boolean continueFlag = true;
        while (continueFlag)
        {
            displayBoard();
            if (gameIsDraw())
            {
                System.out.println("Game Ends in Draw");
                continueFlag = false;
            }
            else
            {
                getPlayerInput(currPlayer);
                if (winner(currPlayer))
                {
                    System.out.println("We have a winner: " + playerID(currPlayer));
                    displayBoard();
                    continueFlag = false;
                }
                else
                {
                    if (currPlayer == player1) currPlayer = player2;
                        else currPlayer = player1;
                }
             }
        }

    }


    public static void main(String[] args)
    {
        TicTacToe game = readTTT();
        boolean restart=false;
        try
        {
            if (game == null)
                game = new TicTacToe();
            else
                restart = true;

            String str;
            do
            {
                game.newgame(restart);
                restart = false;

                System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
                str= game.scan.next();
            } while ("y".equals(str));
        }
        catch (PauseException pe)
        {
            dumpTTT(game);
            System.out.println("Game Paused");
        }

        System.out.println("Bye");
    }
    static TicTacToe readTTT()
    {
        File f = new File("PAUSE");
        if (f.exists() == false)
            return null;

        TicTacToe ttt=null;
        try {
            ObjectInputStream ois = new ObjectInputStream ( new FileInputStream("PAUSE"));
            ttt = (TicTacToe)ois.readObject();
            ois.close();
            f.delete();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ttt;
    }
    static void dumpTTT(TicTacToe ttt)
    {
        try {
            ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("PAUSE"));
            oos.writeObject(ttt);
            oos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}



Last Updated: October 3, 2015 2:29 PM