How to generate combinations from a set of characters

In this post, I shall share a code sample in Java that generates string combinations of characters from a given set of characters over a specified range of lengths of such string combinations. I hope I am not boring you all with this topic,since it’s my third such code sample to this effect. The other two samples can be found here and here.

Note that this is my fastest such code yet. It maxes out the cores of a multi-core CPU on execution. Also note that I have no systematic algorithm for this one! Does that sound weird?

There is no exponentiation involved in this sample as in the one here. We simply map elements in an integer array to characters in a character array and then convert that character array to a string when we want to do something with it.

We manipulate indices in the integer array by incrementation and decrementation, that’s it. The code uses the concurrency APIs of Java and is fully parallelized. It achieved about twenty two million iterations per second during my tests.

Finally, let me say further that I take no responsibility for what is done with this code.

package com.pratyush.stringgenerator2;

import java.util.concurrent.*;

class StringGenerator2 implements Runnable
{
    private static char[] dictionary = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
  
    static Object locker;
  
    static  ExecutorService es;
  
    static int attempts = 0;
  
    private static String getStringFromIndices(int[] indices, char[] str)
    {
    
      for(int z = 0; z < str.length; z++)
      {
          str[z]=dictionary[indices[z]];
      }
    
      return new String(str);
    
    }
  
    private static long getMaxAttempts(int startLen, int stopLen)
    {
        long myMaxAttempts = 0;
    
        for(int c = startLen; c <= stopLen; c++)
        {
            myMaxAttempts += Math.pow(dictionary.length, c); 
        }
    
        return myMaxAttempts;
    }

    public void run()
    {
    
       long start = System.currentTimeMillis();
    
       generate(6, 8); 
    
       long end = System.currentTimeMillis();
    
       double secs = (end - start)/1000;
    
       System.out.println("time = "+secs);
    
       System.out.println("words = "+attempts);
    
       System.out.println("max words = "+ getMaxAttempts(6,8));
    
       double speed = attempts / secs;
    
       System.out.println("speed = "+speed);
    
       synchronized(locker)
       {
        
           es.shutdownNow();
        
           locker.notify();
       }
    
   }
  
   public static void generate(int startLen, int stopLen)
   {
     int LEN = dictionary.length;
    
     attempts = 0;
    
     String strCombo = "";
    
     for(int currLen = startLen; currLen<= stopLen; currLen++)
     {
         char[] str = new char[currLen];
      
         int[] indexes = new int[currLen];
      
         for(int f = 0; f < indexes.length; f++) 
         {
             indexes[f] = 0;
         }
      
         for(int j = 0; j < LEN; j++ )
         {
             indexes[0] = j;
        
             if(currLen == 1)
             {
          
                 strCombo = getStringFromIndices(indexes, str);
          
                 /***generate string here and do whatever you like***/
          
                 attempts++;
             }
             else
             {
                 while(indexes[1]<LEN)
                 {
                     int i = 1;
            
                     for(i = 1; i < currLen; i++)
                     {
                        if(indexes[i] == LEN)
                        {
                            break;
                        }
              
                        if(i == currLen - 1)
                        {
                
                            strCombo = getStringFromIndices(indexes, str);
                                
                            /***generate string here and do whatever you like***/
                                
                             indexes[i]++;
                             attempts++;
                         }
                     }
            
            
                    for(int k = 2;k<currLen;k++)
                    {
                        if(indexes[k] == LEN)
                        {
                            indexes[k-1]++;
                                
                            for(int l = k; l<currLen; l++)
                            {
                                indexes[l] = 0;
                            }
                        }
                    }
                }
          
                for(int m = 1; m < currLen; m++)
                {
                    indexes[m] = 0;
                }
             }
          }
       }
    
    }
  
    public static void main(String[] args)
    {
    
      es = Executors.newSingleThreadExecutor();
    
      locker = new Object();
    
      es.execute(new StringGenerator2());
    
      try{
         synchronized(locker)
         {
            locker.wait();
         }
      }
      catch(InterruptedException ie)
      {
   ie.printStackTrace();
      }
    
   }	
}

 

Code execution after compilation is shown below:

Sample Code Generating String Combinations of Characters

In my post here, I shared an algorithm to generate all possible combinations of a given set of characters for a specified range of lengths. In this post, I shall share a code sample written in Java that implements the algorithm shared before. It does not use any goto keyword and hence does not create sphagetti code.

The code uses the concurrency API of Java and is fully parallelized. It maxes out the CPU cores of a multi-core CPU and achieves a few million iterations per second. The code sample uses a custom utility function ‘power’ for calculating powers of integers. It is made static for more execution speed.
The code is commented in places to help readers understand it.

As with the algorithm, I take no responsibility for what is done with this code.

package stringgenerator;

import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

public class StringGenerator implements Runnable
{
  private static char[] dict = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};  //allowed characters
  
  private static ExecutorService ex;
  
  private static AtomicLong attempts = new AtomicLong(0); //count the number of combinations generated
  
  private static Object locker = new Object();
  
  private static long maxAttempts = 0; //total possible combinations of characters of specified lengths
  
  private static int maxLen = 0;
  
  private static long getMaxAttempts(int radix, int maxLen)
  {
    maxAttempts = (power(radix, maxLen) - 1)*radix /(radix-1);
    
    return maxAttempts;
  }
  
  //utility function to calculate powers of integers
  private static long power(int base, int exp)
  {
    if(exp < 0)
    {
      return 1/power(base, -exp);
    }
    else if(exp == 0)
    {
      return 1;
    }
    else{
      long pow = 1;
      for(int p = 1; p <= exp; p++)
      {
        pow*=base;
      }
      
      return pow;
    }
  }
  
  //the main work horse of this program, breaking all possible numbers into combinations
  private static void resolveRadix(int len, long num)
  {
    long[] retVal = new long[len];
    
    int pow = 0;
    int i = -1;

    int radix = len;
    
    for(i = 0; power(radix ,i ) <= num; i++ );
    
    for(int j = 0; j < len - i; j++)
    {
      retVal[j] = 0;
    }
    
    for(int k = len - i; k < len; k++)
    {
      long t = power(radix, i - 1);
      if(i > 1)
      {
      
        retVal[k] = (long)((num - (num % t)) / t);
      
      }
      else
      {
        retVal[k] = (num % radix);
      }
      num -= (retVal[k] * t);
      
      i--;
    }
    
    char[] combo = new char[len];
    
    for(int j = 0; j < len; j++)
    {
      combo[j] = dict[(int)retVal[j]];
    }
    
    String str = new String(combo);
    
    final long attemptsVal = attempts.incrementAndGet();
    
    attempts.set(attemptsVal);
  }
  
  private void generate()
  {
    for(int length = 1; length <= maxLen; length++)
    {
      int radix = dict.length;
      
      long max = (long)power(radix, length);

      for(int i=0;i<max;i++)
      {
        resolveRadix(radix, i);
      }
    }
    
  }
  
  public void run()
  {
    generate();
    
    synchronized(locker)
    {
      locker.notify();
    }
      
    ex.shutdownNow();
    
  }
  
  public static void main(String[] args)
  {
    
    ex = Executors.newSingleThreadExecutor();
    
    maxLen = 6;
    
    long atts = getMaxAttempts(dict.length, maxLen);
    
    StringGenerator threads = new StringGenerator();
    
    long start = System.currentTimeMillis();  //execution start time
    
    ex.execute(threads); //execute code parallelly
    
    try{
        
      synchronized(locker)
      {
        locker.wait();
      }
    }
    catch(InterruptedException ie)
    {
      ie.printStackTrace();
    }
    
    long attemptsVal = attempts.get(); //final total number of attempts
    
    long end = System.currentTimeMillis(); //execution end time
    
    double speed = (double)((1000 * attemptsVal)/(end - start)); //rate of generating combos
    
    double seconds = (double)(end - start) / 1000;
    
    System.out.println("per sec = "+speed);
    
    System.out.println("sec = "+seconds);
    
    System.out.println("words = "+attemptsVal);
    
    System.out.println("max words = "+atts);
    
  }
}

Execution after compilation is shown below:

GUI Tic-Tac-Toe in Java Revisited

In my post found here I discussed a tic-tac-toe game coded in Java which was a version using a Graphical User Interface. The code listing for the classes and interfaces can be found in the same post. In this post I will discuss a Graphical User Interface (GUI) version of the same application with some additional features. The GUI tic-tac-toe app is based on a console application version found here. I shall only provide the changes and additions to the code listings here. Note that I will use the NetBeans IDE.

We will primarily make changes to the AWTBoard class. After making these changes, the user will be asked whether he really wants to quit when a game is in progress. If he clicks Cancel, the game will continue. If he clicks OK, the game will be terminated. If the game is over or not yet begun, the user will be able to stop the game as usual. See the video below the code listing to understand how it works.

package com.tictactoe.components;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Dell
 */
public class AWTBoard extends Board implements ActionListener{
    
    private Button[][] buttons = new Button[3][3];
    private Frame f;
    private Label l;
    
    private Move currMove;
    private Game myGame;
    
    public AWTBoard()
    {
        super();
        f = new Frame("Tic-Tac-Toe");
        f.setSize(300, 300);
        
        
        Panel topPanel = new Panel();
        
        topPanel.setLayout(new BorderLayout());
        
        Panel p = new Panel();
        
        GridLayout grd = new GridLayout(3,3);
        
        p.setLayout(grd);
        
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++)
            {
                buttons[i][j] = new Button();
                
                buttons[i][j].addActionListener(this);
                
                p.add(buttons[i][j], i, j); //problem
                
            }
        }
        
        topPanel.add(p, BorderLayout.CENTER);
        
        l = new Label();
        
        topPanel.add(l, BorderLayout.SOUTH);
        
        f.add(topPanel);

        /****Begin changes****/
        f.addWindowListener(new WindowAdapter(){ 
            
            public void windowClosing(WindowEvent e){ 
                
                if(myGame.isGameOver() || Game.getMovesTillNow() == 0)
                {
                    f.dispose(); 
                    System.exit(0);
                }else{
                    int result = JOptionPane.showConfirmDialog((Component) null, "Game in progress. Are you sure you want to quit?",
        "Message", JOptionPane.OK_CANCEL_OPTION);
            
                    if(result == 0)
                    {
                        f.dispose(); 
                        System.exit(0);
                    }
            
                }
            } 
            
        });
        /****End changes****/        

        //f.pack();
        f.setBounds(200, 200, f.getWidth(), f.getHeight());
        
        f.setVisible(true);
    }
    
    public void setGame(Game game)
    {
        myGame = game;
    }
    
    public void display()
    {
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++) 
            {  

                buttons[i][j].setLabel( ""+cells[i][j] );
               
            } 
             
        }  
    }
    
    public void markCell(Move move, char mark)
    {
        if(move.isIsValid())
        {
            int i = move.getRow(); 
            int j = move.getColumn(); 
            cells[i - 1][j - 1] = mark;
            
            buttons[i - 1][j - 1].setLabel(""+mark);
        }
        else
        {
            //System.out.println("INVALID MOVE!\n\n");
            l.setText("INVALID MOVE!");
        }
    }
    
    public Label getStatusLabel()
    {
        return l;
    }
    
    public void actionPerformed(ActionEvent e)
    {
        Button b = (Button)e.getSource();
        
        for(int i = 0 ; i &lt; 3; i++ )
        {
            for(int j = 0; j &lt; 3; j++)
            {
                if(b == buttons[i][j])
                {
                    currMove = new Move();
                    currMove.setRow(i + 1);
                    currMove.setColumn(j + 1);
                    currMove.setIsValid(true);
                }
            }
        }
        
        
        synchronized(this)
        {
            this.notify();
        }
        
    }
    
    public Move getCurrMove()
    {
        return currMove;
    }
}

 

Deploying Tic-Tac-Toe Java GUI app

In this post, we deploy our tic-tac-toe Java GUI app so that it can be invoked either by double-clicking it or through the command line. Note: These steps below were performed on a Windows system.

  1. First, assemble all the Java source files in a folder of your choice.
  2. Open your Command Prompt.
  3. Navigate to your chosen folder in the Command Prompt using the cd command. e.g. cd Java\progs\TicTacToeGUIDemo
  4. Compile all the Java source files with javac compiler. e.g. javac -d . *.java.
    * means all Java files in the current folder.
  5. After successful compilation, the root package of your app will appear as a folder. In our case it’s ‘com’.
  6. Now, we need to create a manifest file to help us create a java archive file or JAR file.
    This file can be used to launch the app just by double-clicking on it.
    It can also be invoked using the java command from Command Prompt. e.g. java -jar TicTacToe.jar
  7. Open Notepad and create a blank new file called manifest.mf
  8. In this file, type:Main-Class: com.tictactoe.gameplay.AWTTicTacToe. This is the main class name, qualified by the full package name.
  9. After step 8, be sure to add a blank new line in the text file manifest.mf
  10. Save the manifest file and close it.
  11. Go back to your Command Prompt.
    Type the following: jar cvmf manifest.mf  –jar file name–  –root package folder name–
    e.g. jar cvmf manifest.mf TicTacToe.jar com
  12. You will get some verbose output from the jar command. To open your jar file, you can follow either of the steps in step 6 above.

View the video below to see it all in action.

Coding Tic-Tac-Toe with a Graphical User Interface

In my post found here I discussed a tic-tac-toe game coded in Java which was a console application.
The code listing for the classes and interfaces can be found in the same post. In this post I will discuss a Graphical User Interface (GUI) version of the same application. I shall only provide the changes and additions to the code listings here. Note that I will use the NetBeans IDE.

First I shall discuss the changes to the Board class. Since we shall use inheritance, we shall change the accessibility of the cells from private to protected. This is the only change required. See the listing below.

package com.tictactoe.components;  
/**
 *
 * @author pratyush
 */
public class Board { 
 
    protected static char[][] cells; //change to Board class for inheritance
    
    public Board()
    {
        cells = new char[3][3]; 
        cells[0] = new char[]{' ', ' ', ' '}; 
        cells[1] = new char[]{' ', ' ', ' '}; 
        cells[2] = new char[]{' ', ' ', ' '};
    }
    
    public void display()
    {
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++){ 
        if(cells[i][j] == ' ') 
        { 
          System.out.print((i+1)+", "+(j+1)+"\t"); 
        } 
        else{
          System.out.print(cells[i][j]+"\t"); 
        } 
      } 
      
      System.out.print("\n\n"); 
    } 
  } 
  
  public Move checkAvailable(Move move){ 
  
    int i = move.getRow(); 
    int j = move.getColumn(); 
    boolean boundValid = ((i >= 1 && i<= 3) && ( j >= 1 && j <= 3));
    
        boolean charValid = false; 
        if(boundValid)
        {
            charValid = (cells[i - 1][j - 1] == ' ');
        } 
        boolean netValid = (boundValid && charValid); 
        move.setIsValid(netValid); 
        return move;
    }
    
    public void markCell(Move move, char mark)
    {
        if(move.isIsValid())
        {
            int i = move.getRow(); 
            int j = move.getColumn(); 
            cells[i - 1][j - 1] = mark;
        }
        else
        {
            System.out.println("INVALID MOVE!\n\n");
        }
    } 
}

 

AWTBoard

Among the additions, we shall make a new Board class called AWTBoard which shall inherit all the properties of the Board class. However it shall add the functionalities of GUI from the Java Abstract Windowing Toolkit or AWT packages. The listing of this new class is given below. The AWT version of the Board will be a frame with a 3 X 3 grid of buttons with a status label at the bottom to display all the messages which were being outputted to the console in the console version of the application. The players will play their turns by clicking buttons in the grid.

/*
 * 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 com.tictactoe.components;

import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Dell
 */
public class AWTBoard extends Board implements ActionListener{
    
    private Button[][] buttons = new Button[3][3];
    private Frame f;
    private Label l;
    
    private Move currMove;
    
    public AWTBoard()
    {
        super();
        f = new Frame("Tic-Tac-Toe");
        f.setSize(300, 300);
        
        
        Panel topPanel = new Panel();
        
        topPanel.setLayout(new BorderLayout());
        
        Panel p = new Panel();
        
        GridLayout grd = new GridLayout(3,3);
        
        p.setLayout(grd);
        
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++)
            {
                buttons[i][j] = new Button();
                
                buttons[i][j].addActionListener(this);
                
                p.add(buttons[i][j], i, j);
                
            }
        }
        
        topPanel.add(p, BorderLayout.CENTER);
        
        l = new Label();
        
        topPanel.add(l, BorderLayout.SOUTH);
        
        f.add(topPanel);
        
        f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.dispose(); System.exit(0);} });
        
        
        f.setBounds(200, 200, f.getWidth(), f.getHeight());
        
        f.setVisible(true);
    }
    
    public void display()
    {
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++) 
            {  

                buttons[i][j].setLabel( ""+cells[i][j] );
               
            } 
             
        }  
    }
    
    public void markCell(Move move, char mark)
    {
        if(move.isIsValid())
        {
            int i = move.getRow(); 
            int j = move.getColumn(); 
            cells[i - 1][j - 1] = mark;
            
            buttons[i - 1][j - 1].setLabel(""+mark);
        }
        else
        {
            
            l.setText("INVALID MOVE!");
        }
    }
    
    public Label getStatusLabel()
    {
        return l;
    }
    
    public void actionPerformed(ActionEvent e)
    {
        Button b = (Button)e.getSource();
        
        for(int i = 0 ; i &lt; 3; i++ )
        {
            for(int j = 0; j &lt; 3; j++)
            {
                if(b == buttons[i][j])
                {
                    currMove = new Move();
                    currMove.setRow(i + 1);
                    currMove.setColumn(j + 1);
                    currMove.setIsValid(true);
                }
            }
        }
        
        
        synchronized(this)
        {
            this.notify();
        }
        
    }
    
    public Move getCurrMove()
    {
        return currMove;
    }
}

 

First, let us see the constructor of the new Board class AWTBoard. We create a Frame which is basically a window. We then create a Panel and add it to the content pane of the window created previously. We create a Border Layout for the previously added Panel with a top part and a bottom part. To the bottom part, we add a label to display any status messages needed to be displayed. To the top part of the Panel, we create a Panel with a Grid Layout to add a 3 X 3 Grid of Buttons and then add this panel to the main Panel. The Players play the game by taking turns to click the buttons in this grid.

We override the display method of the Board class to mark the buttons of the grid instead of outputting them to the console. Likewise, we override all those other methods which output to the console. The new method added is the actionPerformed method which is basically an event handler that tells Java what to do when the buttons in the 3 x 3 grid are clicked. getStatusLabel and getCurrMove are helper methods to enable passing data in and out of GUI Board.

AWTTicTacToe

Apart from the GUI Board, we need to introduce another class called AWTTicTacToe which will contain changes to invoke the GUI Board instead of the console.

package com.tictactoe.gameplay;

import com.tictactoe.components.*;

/**
 *
 * @author pratyush
 */
public class AWTTicTacToe {  /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        // TODO code application logic here 
        Game game = new Game(); 
        Player player1 = new Player(); 
        player1.setPlayerNum(1); 
        player1.setPlayerMark('X'); 
        player1.setGame(game); 
        Player player2 = new Player(); 
        player2.setPlayerNum(2); 
        player2.setPlayerMark('0'); 
        player2.setGame(game);
        
        //
        AWTBoard board = new AWTBoard();
        board.display();
        
        /*******/ 
        game.setWhoseTurn(player1); 
        game.setGameOver(false); 
        boolean status = game.isGameOver(); 
        // 
        do
        {
            int movesNow = Game.getMovesTillNow(); 
            if(movesNow == 9)
            {
                if(game.getWinner() == null)
                {
                    //
                    board.getStatusLabel().setText("TIE!\n");
                    break;
                }
            } 
            int turn = game.getWhoseTurn(); 
            //
            
            board.getStatusLabel().setText("PLAYER "+turn+" click a button\n"); 
            
            //
            try{
                synchronized(board)
                {
                    board.wait();
                }
            }
            catch(InterruptedException ie)
            {
                ie.printStackTrace();
            }
            
            Move move = board.getCurrMove();
            
            switch(turn)
            {
                case 1:
                    player1.makeMove(board, move); 
                    if(game.checkWinningMoves(player1))
                    {
                        game.setWinner(player1); 
                        //
                        board.getStatusLabel().setText("PLAYER 1 WINS!");
                        game.setGameOver(true);
                    }
                    else
                    {
                        if(move.isIsValid())
                        {
                            movesNow = Game.getMovesTillNow(); 
                            //
                            
                            board.getStatusLabel().setText("MOVE NUMBER "+movesNow+"\n");
                            game.setWhoseTurn(player2);
                        }
                    }
                break;
                case 2:
                    player2.makeMove(board, move); 
                    if(game.checkWinningMoves(player2))
                    {
                        game.setWinner(player2); 
                        //
                        
                        board.getStatusLabel().setText("PLAYER 2 WINS!");
                        game.setGameOver(true);
                    }
                    else
                    {
                        if(move.isIsValid())
                        {
                            movesNow = Game.getMovesTillNow(); 
                            // 
                            board.getStatusLabel().setText("MOVE NUMBER "+movesNow+"\n");
                            game.setWhoseTurn(player1);
                        }
                    }
                break;
                default:
                    //
                    board.getStatusLabel().setText("ERROR!\n");
                break;
            } 
            status = game.isGameOver();
        }
        while(!status); 
    } 
}

 

AWTTicTacToe is the main class containing the main method. It invokes the AWTBoard class instead of the Board class. It will not contain any statements to output to the console, although it may do so for debugging. To see the results of the invocation of this main class see the video below.

 

This GUI implementation of tic-tac-toe in Java is not perfect by any means. There is scope for improvement and new features can be added. Meanwhile, you can run this code and enjoy!

Simple File Searcher App: An Exercise in Concurrency

Here is a simple app for searching files on a computer. The app uses a recursive algorithm to compare names of files and folders with the search keyword and returns all the matches.

The procedure is as follows:

1.	list the roots of the filesystem
2.	for each root listed in step1:
	a.	list folders and files
	b.	for each file in a., compare keyword with file name.  
                If matched, list this file in results 
	c.	for each folder in a, 
		i.  compare keyword with folder name.  If matched, list this folder in results 
		ii. repeat 2a. for each folder
	d.	repeat a. to c. until no subfolders are found

The Java version is given below:

/*
 * 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 com.filesearch.components;

import java.io.*;
import java.io.File.*;
import java.util.concurrent.*;
import java.util.Scanner;
/**
 *
 * @author Dell
 */
public class FileSearcherComponent implements Runnable{
    
    static String fileKeyword;
  
    static long count = 0;
  
    static File logFile;
  
    static BufferedWriter bw;
  
    private void logToFile(String path)
    {
  try{
    
    bw.write(path, 0, path.length());
    bw.newLine();
    
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    }
    
    @Override
    public void run()
    {
  try{
      
    bw = new BufferedWriter(new FileWriter(logFile, true));
      
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    
  long currTime = System.currentTimeMillis();
    
        File[] roots = File.listRoots(); //get drives
        
        for(File root:roots)
        {
            
            File rootFile = new File(root.getPath());
            
            traverseRoots(rootFile);
        }
    
  long endTime = System.currentTimeMillis();
    
  double seconds = (endTime - currTime) / 1000;
    
  System.out.printf("\n %d results in %f seconds.\n", count, seconds);
    
  try{
    
    bw.flush();
    
    bw.close();
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    }
    
    private void traverseRoots(File root)
    {
        if(root.isDirectory())
        {
            
            String name = root.getName();

            if(name.contains(fileKeyword))
            {
                System.out.println(root.getAbsolutePath());
        
    logToFile(root.getAbsolutePath());
        
    count++;
            }
            
            File[] rootFolders = root.listFiles();

            if(rootFolders != null &amp;&amp; rootFolders.length &gt; 0){

                for(File rootFolder: rootFolders)
                {
                    if(rootFolder.isDirectory())
                    {
                        
                        String name1 = rootFolder.getName();

                        if(name1.contains(fileKeyword))
                        {
                            System.out.println(rootFolder.getAbsolutePath());
              
          logToFile(rootFolder.getAbsolutePath());
              
          count++;
                        }
                        
                        traverseRoots(rootFolder);
                    }
                    else if(rootFolder.isFile())
                    {
                        String name2 = rootFolder.getName();

                        if(name2.contains(fileKeyword))
                        {
                            System.out.println(rootFolder.getAbsolutePath());
              
          logToFile(rootFolder.getAbsolutePath());
              
          count++;
                        }
                    }
                    
                }
            }
        }
        else if(root.isFile())
        {
            String name3 = root.getName();

            if(name3.contains(fileKeyword))
            {
                System.out.println(root.getAbsolutePath());
        
    logToFile(root.getAbsolutePath());
        
    count++;
        
            }
        }
       
    }
    
    public static void main(String[] args)
    {
        FileSearcherComponent f = new FileSearcherComponent();
        
        ExecutorService es = Executors.newSingleThreadExecutor();
    
  logFile = new File("LOG.txt");
    
  if(logFile.exists())
  {
    logFile.delete();
  }
    
  try{
    
    logFile.createNewFile();
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    
        System.out.print("Enter search keyword: ");
        
        fileKeyword  = new Scanner(System.in).nextLine();
    
        es.execute(f);	
    
    
    }
    
}

 

Copy, compile, run and ENJOY!

Coding a game of tic-tac-toe using 3 different languages

I used a brute force logic to develop an algorithm for tic-tac-toe.
Firstly, there are only 8 triads of winning points or moves: three vertical, three horizontal and 2 diagonals.
Players play taking turns or making moves alternately. Player 1 makes a maximum of 5 moves and player
2 makes a maximum of 4 moves because there are a maximum of 9 moves to be made and player 1 begins the game.

Furthermore, player 1 has already made 2 moves before getting his first chance to form a winning line.
Players take turns alternately which means that when player 1 gets his first chance to form a line,
player 2 has already made 2 moves of his own. So player 1 can only form a line from move 5. Player 2
can only form his first line at move 6. Player 1 gets his second chance to win at move 7 and his third
and last chance at move 9. Similarly, player 2 gets his second and last chance at move 8.

Every time there is a chance to form a winning line, all possible permutations and combinations of
the current players moves, taken three at a time, are compared with all 8 winning triads. If a match is found,
the current player wins. If player 1 forms a line first, player 2 does not get a chance to continue the game.

The game has 3 versions: PHP, Java and C#

PHP Version

It can be played via the command-line executable of your PHP installation.

In the game of tic-tac-toe there is a 3X3 grid or board.The game is for two players. The players 1 and 2 take turns to mark the cells of the grid with their mark which is a zero or a cross.

The first player to complete a vertical, horizontal or diagonal triad of zeroes or crosses, wins.

In this implementation, I have used the OOPs or Object Oriented Programming features of PHP.

I have divided the functionality and roles or responsibilities between various classes as follows.

Player makes Move on Board to play Game. So the classes are:

  1. Move
  2. Board
  3. Player
  4. Game

First we discuss the move class:

Move:

Move made by either of the players.

class Move
{
  private $row; // between 1 and 3
  private $column; //between 1 and 3 on the 3x3 board
  private $isValid; //whether the move is valid or not
  
  //getters and setters for the properties
  public function setRow($i)
  {
    $this->row = $i;
  }
  
  public function setColumn($j)
  {
    $this->column = $j;
  }
  
  public function setValid($valid)
  {
    $this->isValid = $valid;
  }
  
  public function getRow()
  {
    return $this->row;
  }
  
  public function getColumn()
  {
    return $this->column;
  }
  
  public function getValid()
  {
    return $this->isValid;
  }
}

 

Then comes the Board class where the moves are made.

Board:

The board on which the game is played.

class Board
{
  private $cells = array(); //the 3x3 grid of cells
  
  public function __construct()
  {
    $row1 = array(" ", " ", " ");
    
    $row2 = array(" ", " ", " ");
    
    $row3 = array(" ", " ", " ");
    
    $this->cells[0] = $row1;
    
    $this->cells[1] = $row2;
    
    $this->cells[2] = $row3;
  }
  
  //display the board with moves made so far
  public function display()
  {
    for($i = 0; $i < 3; $i++)
    {
      for($j = 0; $j < 3; $j++) { if($this->cells[$i][$j] == " ")
        {
          echo ($i + 1).", ".($j + 1)."\t";
        }
        else
        {
          echo $this->cells[$i][$j]."\t";
        }
      }
      
      echo "\n\n";
    }
  }
  
  //check if a cell is already occupied or not
  public function checkAvailable($move)
  {
    $i = $move->getRow();
    $j = $move->getColumn();
    
    $valid1 = (($i >= 1 && $i <= 3) && ($j >= 1 && $j <= 3));
    
    if($valid1)
    {
      $valid2 = ($this->cells[$i - 1][$j - 1] == " ");
    }
    else
    {
      $valid2 = false;
    } 

                $valid3 = ($valid1 && $valid2); 

                $move->setValid($valid3);
    
    return $move;
  }

  //put a cross or zero in a cell if the move is valid
  public function markCell($move, $mark)
  {
    if($move->getValid())
    {
      $i = $move->getRow();
      $j = $move->getColumn();
      
      $this->cells[$i - 1][$j - 1] = $mark;
    }
    else
    {
      echo "INVALID MOVE!\n\n";
    }
  }
}

 

The Player class denotes the players who make moves on the board.

Player:

Either of the two players.

class Player
{
  private $playerNum;  //player 1 or 2
  private $playerMark; // '0' or 'X'
  private $winnerOrLoser;
  private $moves = array(); //list of moves made by player
  private $g; //the game being played
  
  //getters and setters
  public function setPlayerNum($num)
  {
    $this->playerNum = $num;
  }

  public function setPlayerMark($mark)
  {
    $this->playerMark = $mark;
  }
  
  public function setPlayerWinnerLoser($winnerOrLoser)
  {
    $this->winnerOrLoser = $winnerOrLoser;
  }
  
  public function getPlayerNum()
  {
    return $this->playerNum;
  }
  
  public function getPlayerMark()
  {
    return $this->playerMark;
  }
  
  public function getPlayerWinLose()
  {
    return $this->winnerOrLoser;
  }
  
  public function getPlayerMoves()
  {
    return $this->moves;
  }
  
  //the player can put x or 0 by choosing the cell (row and column)
  public function makeMove($board, $move)
  {
    $move = $board->checkAvailable($move);
    
    $mark = $this->getPlayerMark();
    $board->markCell($move, $mark);
      
    if($move->getValid())
    {	
      $this->addMove($move);
      
      $this->getGame()->setMovesTillNow();
    }
    
    $board->display();
  }
  
  //if move is valid, add the move to the player's list of moves
  public function addMove($move)
  {
    $this->moves[] = $move;
  }
  
  public function setGame($game)
  {
    $this->g = $game;
    
  }
  
  public function getGame()
  {
    return $this->g;
  }
}

 

The Game class is the functionality of the game with all its rules.

Game:

The game being played.

class Game
{
  private $whoseTurn; //player 1 or 2
  private static $movesTillNow = 0; //how many moves till now
  private $gameOver; //is game over yet?
  private $winningMoves; //list of combination of moves which can win the game
  private $winner = null;
  
  private $winning_moves;
  
  public function __construct()
  {
    //winning moves

    $this->winning_moves = array();
    //row wise
    
    $move01 = new Move();
    $move01->setRow(1);
    $move01->setColumn(1);
    
    $move02 = new Move();
    $move02->setRow(1);
    $move02->setColumn(2);
    
    $move03 = new Move();
    $move03->setRow(1);
    $move03->setColumn(3);
    
    $move0 = array($move01, $move02, $move03);

    $this->winning_moves[0] = $move0;
    
    $move11 = new Move();
    $move11->setRow(2);
    $move11->setColumn(1);
    
    $move12 = new Move();
    $move12->setRow(2);
    $move12->setColumn(2);
    
    $move13 = new Move();
    $move13->setRow(2);
    $move13->setColumn(3);
    
    $move1 = array($move11, $move12, $move13);

    $this->winning_moves[1] = $move1;
    
    $move21 = new Move();
    $move21->setRow(3);
    $move21->setColumn(1);
    
    $move22 = new Move();
    $move22->setRow(3);
    $move22->setColumn(2);
    
    $move23 = new Move();
    $move23->setRow(3);
    $move23->setColumn(3);
    

    $move2 = array($move21, $move22, $move23);

    $this->winning_moves[2] = $move2;

    //column wise
    
    $move31 = new Move();
    $move31->setRow(1);
    $move31->setColumn(1);
    
    $move32 = new Move();
    $move32->setRow(2);
    $move32->setColumn(1);
    
    $move33 = new Move();
    $move33->setRow(3);
    $move33->setColumn(1);
    
    $move3 = array($move31, $move32, $move33);

    $this->winning_moves[3] = $move3;
    
    $move41 = new Move();
    $move41->setRow(1);
    $move41->setColumn(2);
    
    $move42 = new Move();
    $move42->setRow(2);
    $move42->setColumn(2);
    
    $move43 = new Move();
    $move43->setRow(3);
    $move43->setColumn(2);
    
    $move4 = array($move41, $move42, $move43);

    $this->winning_moves[4] = $move4;
    
    $move51 = new Move();
    $move51->setRow(1);
    $move51->setColumn(3);
    
    $move52 = new Move();
    $move52->setRow(2);
    $move52->setColumn(3);
    
    $move53 = new Move();
    $move53->setRow(3);
    $move53->setColumn(3);
    
    $move5 = array($move51, $move52, $move53);

    $this->winning_moves[5] = $move5;

    //diagonally
    
    $move61 = new Move();
    $move61->setRow(1);
    $move61->setColumn(1);
    
    $move62 = new Move();
    $move62->setRow(2);
    $move62->setColumn(2);
    
    $move63 = new Move();
    $move63->setRow(3);
    $move63->setColumn(3);
    
    $move6 = array($move61, $move62, $move63);

    $this->winning_moves[6] = $move6;
    
    $move71 = new Move();
    $move71->setRow(1);
    $move71->setColumn(3);
    
    $move72 = new Move();
    $move72->setRow(2);
    $move72->setColumn(2);
    
    $move73 = new Move();
    $move73->setRow(3);
    $move73->setColumn(1);
    
    $move7 = array($move71, $move72, $move73);

    $this->winning_moves[7] = $move7;
  }
  
  //getters and setters
  public function getWhoseTurn()
  {
    return $this->whoseTurn;
  }	
  
  public function setWhoseTurn($player)
  {
    $this->whoseTurn = $player->getPlayerNum();
  }
  
  public function getGameOver()
  {
    return $this->gameOver;
  }	
  
  public function setGameOver($gameOver)
  {
    $this->gameOver = $gameOver;
  }
  
  public function setMovesTillNow()
  {
    self::$movesTillNow++;
  }
  
  public function getMovesTillNow()
  {
    return self::$movesTillNow;
  }
  
  public function setWinner($winner)
  {
    $this->winner = $winner;
  }
  
  public function getWinner()
  {
    return $this->winner;
  }
  
  //check if winning moves have been made by either player
  public function checkWinningMoves($player)
  {
    
    $movesTillNow = $this->getMovesTillNow();
    
    $whoseTurn = $this->getWhoseTurn();
    
    $playerMoves = $player->getPlayerMoves();
    
    $moves = $movesTillNow;
    
    $winning_moves1 = $this->winning_moves;
    
    switch($whoseTurn)
    {
      case 1:
        
        if($moves < 5)
        {
          return false;
        }
        else
        {
          
          if($moves == 5)
          {
          
            $player1_set1 = array($playerMoves[0], $playerMoves[1], $playerMoves[2]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player1_set1, $winning_moves1[$i]))
              {
                return true;
              }
            }
            
          }
          
          if($moves == 7)
          {
            $player1_set2 = array($playerMoves[0], $playerMoves[1], $playerMoves[3]);
            
            $player1_set4 = array($playerMoves[0], $playerMoves[2], $playerMoves[3]);
            
            $player1_set7 = array($playerMoves[1], $playerMoves[2], $playerMoves[3]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player1_set2, $winning_moves1[$i]) || 
              $this->checkSubset($player1_set4, $winning_moves1[$i]) || 
              $this->checkSubset($player1_set7, $winning_moves1[$i]) )
              {
                return true;
              }
            }
          }
          
          if($moves == 9)
          {
          
            $player1_set3 = array($playerMoves[0], $playerMoves[1], $playerMoves[4]);
            
            $player1_set5 = array($playerMoves[0], $playerMoves[2], $playerMoves[4]);
            
            $player1_set6 = array($playerMoves[0], $playerMoves[3], $playerMoves[4]);
            
            $player1_set8 = array($playerMoves[1], $playerMoves[2], $playerMoves[4]);
            
            $player1_set9 = array($playerMoves[1], $playerMoves[3], $playerMoves[4]);
            
            $player1_set10 = array($playerMoves[2], $playerMoves[3], $playerMoves[4]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player1_set3, $winning_moves1[$i]) ||
              $this->checkSubset($player1_set5, $winning_moves1[$i]) || $this->checkSubset($player1_set6, $winning_moves1[$i]) || 
              $this->checkSubset($player1_set8, $winning_moves1[$i]) || $this->checkSubset($player1_set9, $winning_moves1[$i]) || 
              $this->checkSubset($player1_set10, $winning_moves1[$i]))
              {
                return true;
              }
            }
            
          }
          
          return false;
           
          
        
        }
      break;
      case 2:
        if($moves < 6)
        {
          return false;
        }
        else
        {
          if($moves == 6)
          {
            $player2_set1 = array($playerMoves[0], $playerMoves[1], $playerMoves[2]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player2_set1, $winning_moves1[$i]))
              {
                return true;
              }
            }
          }
          
          if($moves == 8)
          {
    
            $player2_set2 = array($playerMoves[0], $playerMoves[1], $playerMoves[3]);
            
            $player2_set3 = array($playerMoves[0], $playerMoves[2], $playerMoves[3]);
            
            $player2_set4 = array($playerMoves[1], $playerMoves[2], $playerMoves[3]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player2_set2, $winning_moves1[$i]) || $this->checkSubset($player2_set3, $winning_moves1[$i]) ||
              $this->checkSubset($player2_set4, $winning_moves1[$i]))
              {
                return true;
              }
            }
          }
          
          
          
          return false;
        
        }
      break;
      default:
        return false;
    }
    
    
  }
  
  //check if player moves match the winning sequence
  private function checkSubset($subset, $winning_row)
  {
    
    $count = 0;
    for($i = 0; $i < 3; $i++)
    {
      
      for($j = 0; $j < 3; $j++) { if($subset[$i]->getRow() == $winning_row[$j]->getRow() && $subset[$i]->getColumn() == $winning_row[$j]->getColumn())
        {
          $count++;
        }
      }
      
    }
    return ($count == 3);
    
  }
}

 

Putting it all together.

<?php

require("includes/move.php");  //the Move class

require("includes/player.php"); //the Player class

require("includes/board.php"); //the Board class

require("includes/game.php"); //the Game class

function main() //where all the action is
{
  $game = new Game();
  
  $player1 = new Player();
  
  $player1->setPlayerNum(1);
  
  $player1->setPlayerMark('X');
  
  $player1->setGame($game);
  
  $player2 = new Player();
  
  $player2->setPlayerNum(2);
  
  $player2->setPlayerMark('0');
  
  $player2->setGame($game);
  
  $b = new Board();

  $b->display();
  
  $game->setWhoseTurn($player1);
  
  $game->setGameOver(false);
  
  $status = $game->getGameOver();
  
  do
  {
    $movesNow = $game->getMovesTillNow();
    
    if($movesNow == 9)
    {
      
      if($game->getWinner() == null)
      {
        echo "TIE!";
        break;
      }
      
    }
    
    $turn = $game->getWhoseTurn();
    
    echo "PLAYER $turn:\n\n";
    
    $row = 0; $column = 0;
    
    echo "Enter row: ";
    
    trim(fscanf(STDIN, "%d\n", $row));
    
    echo "Enter column: ";
    
    trim(fscanf(STDIN, "%d\n", $column));
    
    echo "\n";
    
    $move = new Move();
    
    $move->setRow($row);
    
    $move->setColumn($column);
    
    switch($turn)
    {
      case 1:
        $player1->makeMove($b, $move);
        
        if($game->checkWinningMoves($player1))
        {
          
          $game->setWinner($player1);
          
          echo "PLAYER 1 WINS!\n\n";
          
          $game->setGameOver(true);
        }
        else
        {
          if($move->getValid())
          {
            $movesNow = $game->getMovesTillNow();
    
            echo "MOVE NUMBER: $movesNow\n\n";
            
            $game->setWhoseTurn($player2);
            
          }
        }
      break;
      case 2:
        $player2->makeMove($b, $move);
        
        if($game->checkWinningMoves($player2))
        {
          
          $game->setWinner($player2);
          
          echo "PLAYER 2 WINS!\n\n";
          
          $game->setGameOver(true);
        }
        else
        {
          if($move->getValid())
          {
            $movesNow = $game->getMovesTillNow();
    
            echo "MOVE NUMBER: $movesNow\n\n";
            
            $game->setWhoseTurn($player1);
          }
        }
      break;
      default:
        echo "ERROR!\n\n";
      break;
    }
    
    $status = $game->getGameOver();
    
  }while(!$status);
  
}

main(); //invoking the game play

?>

 

 

This implementation is not perfect by any means. Any suggestions for improvement are welcome. Screenshots given below:

Java Version

Here is an elementary tic-tac-toe or noughts and crosses game made in Java.
It can be played via the java interpreter from the command line. In the game of tic-tac-toe there is a 3X3 grid or board.The game is for two players. The players take turns to mark the cells of the grid with their mark which is a zero or a cross.

The first player to complete a vertical, horizontal or diagonal triad of zeroes or crosses, wins.

The PHP version can be found here

Player makes Move on Board to play Game. So the classes are:

  1. Move
  2. Board
  3. Player
  4. Game
  5. TicTacToe

There is one new class “TicTacToe” because Java needs a main class having the “main” method implemented in it.

The first four classes are in the package named “com.tictactoe.components” , while the main class is in the package named “com.tictactoe”.

First we discuss the move class:

Move:

Move made by either of the players.

package com.tictactoe.components;

/**
 *
 * @author pratyush
 */
public class Move {
    
    private int row, column;
    private boolean isValid;

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public boolean isIsValid() {
        return isValid;
    }

    public void setIsValid(boolean isValid) {
        this.isValid = isValid;
    }
    
}

 

Then comes the Board class where the moves are made.

Board:

The board on which the game is played.

package com.tictactoe.components;

/**
 *
 * @author pratyush
 */
public class Board{
    
    private static char[][] cells;
    
    public Board()
    {
        cells = new char[3][3];
        
        cells[0] = new char[]{' ', ' ', ' '};
        
        cells[1] = new char[]{' ', ' ', ' '};
        
        cells[2] = new char[]{' ', ' ', ' '};
    }
    
    
   public void display()
   {
       for(int i = 0; i < 3; i++)
       {
           for(int j = 0; j < 3; j++) { 

                 if(cells[i][j] == ' ') { 

                       System.out.print((i+1)+", "+(j+1)+"\t"); 

                 } else { 

                       System.out.print(cells[i][j]+"\t");
  
                 } 

            } System.out.print("\n\n"); 

       } 

    } 

    public Move checkAvailable(Move move) { 

       int i = move.getRow(); 

       int j = move.getColumn(); 

       boolean boundValid = ((i >= 1 && i<= 3) && ( j >= 1 && j <= 3));
       
       boolean charValid = false;
       
       if(boundValid)
       {
           charValid = (cells[i - 1][j - 1] == ' ');
       }
       
       boolean netValid = (boundValid && charValid);
       
       move.setIsValid(netValid);
       
       return move;
   }
   
   public void markCell(Move move, char mark)
   {
       if(move.isIsValid())
       {
           int i = move.getRow();
           
           int j = move.getColumn();
           
           cells[i - 1][j - 1] = mark;
       }
       else
       {
           System.out.println("INVALID MOVE!\n\n");
       }
   }
   
}

 

The Player class denotes the players who make moves on the board.

Player:

Either of the two players.

package com.tictactoe.components;

import java.util.ArrayList;
/**
 *
 * @author pratyush
 */
public class Player {
    
    private int playerNum;
    private char playerMark;
    private int winnerOrLoser;
    private Game g;
    
    private ArrayList<Move> moves = new ArrayList<Move>();

    public int getPlayerNum() {
        return playerNum;
    }

    public void setPlayerNum(int playerNum) {
        this.playerNum = playerNum;
    }

    public char getPlayerMark() {
        return playerMark;
    }

    public void setPlayerMark(char playerMark) {
        this.playerMark = playerMark;
    }

    public int getWinnerOrLoser() {
        return winnerOrLoser;
    }

    public void setWinnerOrLoser(int winnerOrLoser) {
        this.winnerOrLoser = winnerOrLoser;
    }

    public Game getGame() {
        return g;
    }

    public void setGame(Game g) {
        this.g = g;
    }

    public ArrayList getMoves() {
        return moves;
    }

    public void addMove(Move move) {
        
        this.moves.add(move);
    }
    
    public void makeMove(Board board, Move move)
    {
        move = board.checkAvailable(move);
        
        char mark = this.getPlayerMark();
        
        board.markCell(move, mark);
        
        if(move.isIsValid())
        {
            this.addMove(move);
            Game.setMovesTillNow();
        }
        
        board.display();
    }
    
}

 

The Game class is the functionality of the game with all its rules.

Game:

The game being played.

package com.tictactoe.components;

import java.util.ArrayList;
/**
 *
 * @author pratyush
 */
public class Game{
 
    private int whoseTurn;
    private static int movesTillNow = 0;
    private boolean gameOver;
    
    private Player winner = null;
    
    Move[][] winning_moves;
    
    public Game()
    {
        
        winning_moves = new Move[8][3];
        
        //top row
        Move move01 = new Move();
        move01.setRow(1);
        move01.setColumn(1);
        
        Move move02 = new Move();
        move02.setRow(1);
        move02.setColumn(2);
        
        Move move03 = new Move();
        move03.setRow(1);
        move03.setColumn(3);
        
        Move[] move0;
        
        move0 = new Move[]{move01, move02, move03};
        
        
        winning_moves[0] = move0;
        
        //middle row
        
        Move move11 = new Move();
        move11.setRow(2);
        move11.setColumn(1);
        
        Move move12 = new Move();
        move12.setRow(2);
        move12.setColumn(2);
        
        Move move13 = new Move();
        move13.setRow(2);
        move13.setColumn(3);
        
        Move[] move1;
        
        move1 = new Move[]{move11, move12, move13};
        
        winning_moves[1] = move1;
        
        //bottom row
        
        Move move21 = new Move();
        move21.setRow(3);
        move21.setColumn(1);
        
        Move move22 = new Move();
        move22.setRow(3);
        move22.setColumn(2);
        
        Move move23 = new Move();
        move23.setRow(3);
        move23.setColumn(3);
        
        Move[] move2;
        
        move2 = new Move[]{move21, move22, move23};
        
        winning_moves[2] = move2;
        
        //column wise
        
        //column 1
        
        Move move31 = new Move();
        move31.setRow(1);
        move31.setColumn(1);
        
        Move move32 = new Move();
        move32.setRow(2);
        move32.setColumn(1);
        
        Move move33 = new Move();
        move33.setRow(3);
        move33.setColumn(1);
        
        Move[] move3;
        
        move3 = new Move[]{move31, move32, move33};
        
        winning_moves[3] = move3;
        
        //column 2
        
        Move move41 = new Move();
        move41.setRow(1);
        move41.setColumn(2);
        
        Move move42 = new Move();
        move42.setRow(2);
        move42.setColumn(2);
        
        Move move43 = new Move();
        move43.setRow(3);
        move43.setColumn(2);
        
        Move[] move4;
        
        move4 = new Move[]{move41, move42, move43};
        
        winning_moves[4] = move4;
        
        //column 3
        
        Move move51 = new Move();
        move51.setRow(1);
        move51.setColumn(3);
        
        Move move52 = new Move();
        move52.setRow(2);
        move52.setColumn(3);
        
        Move move53 = new Move();
        move53.setRow(3);
        move53.setColumn(3);
        
        Move[] move5;
        
        move5 = new Move[]{move51, move52, move53};
        
        winning_moves[5] = move5;
        
        //diagonals
        
        //diagonal 1
        
        Move move61 = new Move();
        move61.setRow(1);
        move61.setColumn(1);
        
        Move move62 = new Move();
        move62.setRow(2);
        move62.setColumn(2);
        
        Move move63 = new Move();
        move63.setRow(3);
        move63.setColumn(3);
        
        Move[] move6;
        
        move6 = new Move[]{move61, move62, move63};
        
        winning_moves[6] = move6;
        
        //diagonal 2
        
        Move move71 = new Move();
        move71.setRow(1);
        move71.setColumn(3);
        
        Move move72 = new Move();
        move72.setRow(2);
        move72.setColumn(2);
        
        Move move73 = new Move();
        move73.setRow(3);
        move73.setColumn(1);
        
        Move[] move7;
        
        move7 = new Move[]{move71, move72, move73};
        
        winning_moves[7] = move7;
        
    }

    public int getWhoseTurn() {
        return whoseTurn;
    }

    public void setWhoseTurn(Player player) {
        this.whoseTurn = player.getPlayerNum();
    }

    public static int getMovesTillNow() {
        return movesTillNow;
    }

    public static void setMovesTillNow() {
        Game.movesTillNow++;
    }

    public boolean isGameOver() {
        return gameOver;
    }

    public void setGameOver(boolean gameOver) {
        this.gameOver = gameOver;
    }

    public Player getWinner() {
        return winner;
    }

    public void setWinner(Player winner) {
        this.winner = winner;
    }
    
    private boolean checkSubset(Move[] subset, Move[] winning_row)
    {
        int count = 0;
        
        for(int i = 0;i < 3; i++)
        {
            for(int j = 0; j< 3; j++)
            {
                if(subset[i].getRow() == winning_row[j].getRow() && subset[i].getColumn() == winning_row[j].getColumn())
                {
                   count++; 
                }
            }
        }
        
        return (count == 3);
    }
    
    
    //check the winning moves
    
    public boolean checkWinningMoves(Player player)
    {
        
        /*****check winning moves*****/
        
        int movesTillNow = Game.getMovesTillNow();
        
        int whoseTurn = this.getWhoseTurn();
        
        ArrayList<Move> playerMoves = player.getMoves();
        
        int moves = movesTillNow;
        
        Move[][] winning_moves1 = this.winning_moves;
        
        switch(whoseTurn)
        {
            case 1:
                
                if(moves < 5)
                {
                    return false;
                }
                else
                {
                    if(moves == 5)
                    {
                        Move[] player1_set1 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(2)};
                        
                        for(int i = 0; i< 8; i++)
                        {
                            if(checkSubset(player1_set1, winning_moves1[i]))
                            {
                                return true;
                            }
                        }
                    }
                    
                    /******/
                    
                    if(moves == 7)
                    {
                        Move[] player1_set2 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(3)};
            
                        Move[] player1_set4 = new Move[]{playerMoves.get(0), playerMoves.get(2), playerMoves.get(3)};

                        Move[] player1_set7 = new Move[]{playerMoves.get(1), playerMoves.get(2), playerMoves.get(3)};

                        for(int i = 0; i < 8; i++)
                        {
                                if(checkSubset(player1_set2, winning_moves1[i]) || 
                                checkSubset(player1_set4, winning_moves1[i]) || 
                                checkSubset(player1_set7, winning_moves1[i]) )
                                {
                                        return true;
                                }
                        }
                    }
                    
                    if(moves == 9)
                    {
                        Move[] player1_set3 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(4)};
            
                        Move[] player1_set5 = new Move[]{playerMoves.get(0), playerMoves.get(2), playerMoves.get(4)};

                        Move[] player1_set6 = new Move[]{playerMoves.get(0), playerMoves.get(3), playerMoves.get(4)};

                        Move[] player1_set8 = new Move[]{playerMoves.get(1), playerMoves.get(2), playerMoves.get(4)};

                        Move[] player1_set9 = new Move[]{playerMoves.get(1), playerMoves.get(3), playerMoves.get(4)};

                        Move[] player1_set10 = new Move[]{playerMoves.get(2), playerMoves.get(3), playerMoves.get(4)};

                        for(int i = 0; i < 8; i++)
                        {
                                if(checkSubset(player1_set3, winning_moves1[i]) ||
                                checkSubset(player1_set5, winning_moves1[i]) || checkSubset(player1_set6, winning_moves1[i]) || 
                                checkSubset(player1_set8, winning_moves1[i]) || checkSubset(player1_set9, winning_moves1[i]) || 
                                checkSubset(player1_set10, winning_moves1[i]))
                                {
                                        return true;
                                }
                        }
                    }
                    
                    return false;
                }
                
            case 2:
                /******/
                if(moves < 6)
                {
                    return false;
                }
                else
                {
                    /*******/
                    if(moves == 6)
                    {
                        Move[] player2_set1 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(2)};
            
                        for(int i = 0; i < 8; i++)
                        {
                                if(checkSubset(player2_set1, winning_moves1[i]))
                                {
                                        return true;
                                }
                        }
                    }
                    
                    if(moves == 8)
                    {
                        Move[] player2_set2 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(3)};
            
                        Move[] player2_set3 = new Move[]{playerMoves.get(0), playerMoves.get(2), playerMoves.get(3)};

                        Move[] player2_set4 = new Move[]{playerMoves.get(1), playerMoves.get(2), playerMoves.get(3)};

                        for(int i = 0; i < 8; i++)
                        {
                            if(checkSubset(player2_set2, winning_moves1[i]) || checkSubset(player2_set3, winning_moves1[i]) ||
                            checkSubset(player2_set4, winning_moves1[i]))
                            {
                                    return true;
                            }
                        }
                    }
                    
                    return false;
                }
            default:
                return false;
        }
    }
    
}

 

Finally, the main class:

TicTacToe

The class containing the entry point of the program also called the main class.

package com.tictactoe;

import com.tictactoe.components.*;
import java.util.Scanner;
/**
 *
 * @author pratyush
 */
public class TicTacToe{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         // TODO code application logic here
        
        Game game = new Game();
        
        Player player1 = new Player();
        
        player1.setPlayerNum(1);
        
        player1.setPlayerMark('X');
        
        player1.setGame(game);
        
        Player player2 = new Player();
        
        player2.setPlayerNum(2);
        
        player2.setPlayerMark('0');
        
        player2.setGame(game);
        
        Board board = new Board();
        
        board.display();
        
        /*******/
        
        game.setWhoseTurn(player1);
        
        game.setGameOver(false);
        
        boolean status = game.isGameOver();
        
        Scanner sc = new Scanner(System.in);
        
        do
        {
            int movesNow = Game.getMovesTillNow();
            
            if(movesNow == 9)
            {
                if(game.getWinner() == null)
                {
                    System.out.println("TIE!\n");
                    break;
                }
            }
            
            int turn = game.getWhoseTurn();
            
            System.out.println("PLAYER "+turn+"\n");
            
            int row = 0; 
            int column = 0;
      
      boolean invalid = true;
      
      while(invalid)
      {
             //code to handle invalid moves. players cannot proceed without valid input
        try{
          System.out.println("Enter row: ");
              
          String str = sc.next();
              
          if(!str.equals(""))
          {
              
            row = Integer.parseInt(str);
            invalid = false;
              
          }
          else
          {
            continue;
          }
        }
        catch(NumberFormatException n)
        {
          System.out.println("INVALID ROW!");
          invalid = true;
        }
        catch(InputMismatchException i)
        {
          System.out.println("INVALID ROW!");
          invalid = true;
        }
      }
      
      invalid = true;
      
      while(invalid)
      {
        /*code to handle invalid moves. players cannot proceed without valid input*/
          
        try{
          System.out.println("Enter column: ");
            
          String str = sc.next();
            
          if(!str.equals(""))
          {
            
            column = Integer.parseInt(str);
            invalid = false;
            
          }
          else
          {
            continue;
          }
        }
        catch(NumberFormatException n)
        {
          System.out.println("INVALID COLUMN!");
          invalid = true;
        }
        catch(InputMismatchException i)
        {
          System.out.println("INVALID COLUMN!");
          invalid = true;
        }
      }
      
      /**/
            
            Move move = new Move();
            
            move.setRow(row);
            
            move.setColumn(column);
            
            switch(turn)
            {
                case 1:
                    player1.makeMove(board, move);
                    
                    if(game.checkWinningMoves(player1))
                    {
                        game.setWinner(player1);
                        
                        System.out.println("PLAYER 1 WINS!");
                        
                        game.setGameOver(true);
                    }
                    else
                    {
                        if(move.isIsValid())
                        {
                            movesNow = Game.getMovesTillNow();
                            
                            System.out.println("MOVE NUMBER "+movesNow+"\n");
                            
                            game.setWhoseTurn(player2);
                        }
                    }
                    break;
                case 2:
                    player2.makeMove(board, move);
                    
                    if(game.checkWinningMoves(player2))
                    {
                        game.setWinner(player2);
                        
                        System.out.println("PLAYER 2 WINS!");
                        
                        game.setGameOver(true);
                    }
                    else
                    {
                        if(move.isIsValid())
                        {
                            movesNow = Game.getMovesTillNow();
                            
                            System.out.println("MOVE NUMBER "+movesNow+"\n");
                            
                            game.setWhoseTurn(player1);
                        }
                    }
                    break;
                default:
                    System.out.println("ERROR!\n");
                    break;
            }
            
            status = game.isGameOver();
        }
        while(!status);
        
    }
    
}

 

Copy, Compile, Run and ENJOY!

 

C# version

Here is an elementary tic-tac-toe or noughts and crosses game I made in C#, arguably the most popular language of the DotNET platform. It can be played from the command line after compiling it as an executable. In the game of tic-tac-toe there is a 3X3 grid or board.The game is for two players. The players 1 and 2 take turns to mark the cells of the grid with their mark which is a zero or a cross.

The first player to complete a vertical, horizontal or diagonal triad of zeroes or crosses, wins.

The PHP version can be found here

Player makes Move on Board to play Game. So the classes are:

  1. Move
  2. Board
  3. Player
  4. Game
  5. TicTacToe

There is one new class “TicTacToe” because C# needs a main class having the “Main” method implemented in it.

The classes are in the namespace called “TicTacToeDotNet”.

 

First we discuss the move class:

Move:

Move made by either of the players.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class Move
    {

        private int row, column;
        private bool isValid;

        public int getRow()
        {
            return row;
        }

        public void setRow(int row)
        {
            this.row = row;
        }

        public int getColumn()
        {
            return column;
        }

        public void setColumn(int column)
        {
            this.column = column;
        }

        public bool isIsValid()
        {
            return isValid;
        }

        public void setIsValid(bool isValid)
        {
            this.isValid = isValid;
        }

    }
}

 

Then comes the Board class where the moves are made.

Board:

The board on which the game is played.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class Board
    {

        private static char[][] cells;

        public Board()
        {
            cells = new char[3][];

            cells[0] = new char[] { ' ', ' ', ' ' };

            cells[1] = new char[] { ' ', ' ', ' ' };

            cells[2] = new char[] { ' ', ' ', ' ' };
        }


        public void display()
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++) { 

                     if (cells[i][j] == ' ') { 

                          Console.Write((i + 1) + ", " + (j + 1) + "\t"); 

                      } else { 

                           Console.Write(cells[i][j] + "\t"); 

                      } 

                  } Console.Write("\n\n"); 

            } 

      } 

      public Move checkAvailable(Move move) { 

            int i = move.getRow(); 

            int j = move.getColumn(); 

            bool boundValid = ((i >= 1 && i <= 3) && (j >= 1 && j <= 3));

            bool charValid = false;

            if (boundValid)
            {
                charValid = (cells[i - 1][j - 1] == ' ');
            }

            bool netValid = (boundValid && charValid);

            move.setIsValid(netValid);

            return move;
        }

        public void markCell(Move move, char mark)
        {
            if (move.isIsValid())
            {
                int i = move.getRow();

                int j = move.getColumn();

                cells[i - 1][j - 1] = mark;
            }
            else
            {
                Console.Write("INVALID MOVE!\n\n");
            }
        }

    }

}

 

The Player class denotes the players who make moves on the board.

Player:

Either of the two players.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class Player
    {

        private int playerNum;
        private char playerMark;
        private int winnerOrLoser;
        private Game g;

        private List moves = new List();

        public int getPlayerNum()
        {
            return playerNum;
        }

        public void setPlayerNum(int playerNum)
        {
            this.playerNum = playerNum;
        }

        public char getPlayerMark()
        {
            return playerMark;
        }

        public void setPlayerMark(char playerMark)
        {
            this.playerMark = playerMark;
        }

        public int getWinnerOrLoser()
        {
            return winnerOrLoser;
        }

        public void setWinnerOrLoser(int winnerOrLoser)
        {
            this.winnerOrLoser = winnerOrLoser;
        }

        public Game getGame()
        {
            return g;
        }

        public void setGame(Game g)
        {
            this.g = g;
        }

        public List getMoves()
        {
            return moves;
        }

        public void addMove(Move move)
        {

            this.moves.Add(move);
        }

        public void makeMove(Board board, Move move)
        {
            move = board.checkAvailable(move);

            char mark = this.getPlayerMark();

            board.markCell(move, mark);

            if (move.isIsValid())
            {
                this.addMove(move);
                Game.setMovesTillNow();
            }

            board.display();
        }

    }
}

 

The Game class is the functionality of the game with all its rules.

Game:

The game being played.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class Game
    {

        private int whoseTurn;
        private static int movesTillNow = 0;
        private bool gameOver;

        private Player winner = null;

        Move[][] winning_moves;

        public Game()
        {

            winning_moves = new Move[8][];

            //top row
            Move move01 = new Move();
            move01.setRow(1);
            move01.setColumn(1);

            Move move02 = new Move();
            move02.setRow(1);
            move02.setColumn(2);

            Move move03 = new Move();
            move03.setRow(1);
            move03.setColumn(3);

            Move[] move0;

            move0 = new Move[] { move01, move02, move03 };


            winning_moves[0] = move0;

            //middle row

            Move move11 = new Move();
            move11.setRow(2);
            move11.setColumn(1);

            Move move12 = new Move();
            move12.setRow(2);
            move12.setColumn(2);

            Move move13 = new Move();
            move13.setRow(2);
            move13.setColumn(3);

            Move[] move1;

            move1 = new Move[] { move11, move12, move13 };

            winning_moves[1] = move1;

            //bottom row

            Move move21 = new Move();
            move21.setRow(3);
            move21.setColumn(1);

            Move move22 = new Move();
            move22.setRow(3);
            move22.setColumn(2);

            Move move23 = new Move();
            move23.setRow(3);
            move23.setColumn(3);

            Move[] move2;

            move2 = new Move[] { move21, move22, move23 };

            winning_moves[2] = move2;

            //column wise

            //column 1

            Move move31 = new Move();
            move31.setRow(1);
            move31.setColumn(1);

            Move move32 = new Move();
            move32.setRow(2);
            move32.setColumn(1);

            Move move33 = new Move();
            move33.setRow(3);
            move33.setColumn(1);

            Move[] move3;

            move3 = new Move[] { move31, move32, move33 };

            winning_moves[3] = move3;

            //column 2

            Move move41 = new Move();
            move41.setRow(1);
            move41.setColumn(2);

            Move move42 = new Move();
            move42.setRow(2);
            move42.setColumn(2);

            Move move43 = new Move();
            move43.setRow(3);
            move43.setColumn(2);

            Move[] move4;

            move4 = new Move[] { move41, move42, move43 };

            winning_moves[4] = move4;

            //column 3

            Move move51 = new Move();
            move51.setRow(1);
            move51.setColumn(3);

            Move move52 = new Move();
            move52.setRow(2);
            move52.setColumn(3);

            Move move53 = new Move();
            move53.setRow(3);
            move53.setColumn(3);

            Move[] move5;

            move5 = new Move[] { move51, move52, move53 };

            winning_moves[5] = move5;

            //diagonals

            //diagonal 1

            Move move61 = new Move();
            move61.setRow(1);
            move61.setColumn(1);

            Move move62 = new Move();
            move62.setRow(2);
            move62.setColumn(2);

            Move move63 = new Move();
            move63.setRow(3);
            move63.setColumn(3);

            Move[] move6;

            move6 = new Move[] { move61, move62, move63 };

            winning_moves[6] = move6;

            //diagonal 2

            Move move71 = new Move();
            move71.setRow(1);
            move71.setColumn(3);

            Move move72 = new Move();
            move72.setRow(2);
            move72.setColumn(2);

            Move move73 = new Move();
            move73.setRow(3);
            move73.setColumn(1);

            Move[] move7;

            move7 = new Move[] { move71, move72, move73 };

            winning_moves[7] = move7;

        }

        public int getWhoseTurn()
        {
            return whoseTurn;
        }

        public void setWhoseTurn(Player player)
        {
            this.whoseTurn = player.getPlayerNum();
        }

        public static int getMovesTillNow()
        {
            return movesTillNow;
        }

        public static void setMovesTillNow()
        {
            Game.movesTillNow++;
        }

        public bool isGameOver()
        {
            return gameOver;
        }

        public void setGameOver(bool gameOver)
        {
            this.gameOver = gameOver;
        }

        public Player getWinner()
        {
            return winner;
        }

        public void setWinner(Player winner)
        {
            this.winner = winner;
        }

        private bool checkSubset(Move[] subset, Move[] winning_row)
        {
            int count = 0;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (subset[i].getRow() == winning_row[j].getRow() && subset[i].getColumn() == winning_row[j].getColumn())
                    {
                        count++;
                    }
                }
            }

            return (count == 3);
        }


        //check the winning moves

        public bool checkWinningMoves(Player player)
        {

            /*****check winning moves*****/

            int movesTillNow = Game.getMovesTillNow();

            int whoseTurn = this.getWhoseTurn();

            Move[] playerMoves = player.getMoves().ToArray();

            int moves = movesTillNow;

            Move[][] winning_moves1 = this.winning_moves;

            switch (whoseTurn)
            {
                case 1:

                    if (moves < 5)
                    {
                        return false;
                    }
                    else
                    {
                        if (moves == 5)
                        {
                            Move[] player1_set1 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[2] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player1_set1, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        /******/

                        if (moves == 7)
                        {
                            Move[] player1_set2 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[3] };

                            Move[] player1_set4 = new Move[] { playerMoves[0], playerMoves[2], playerMoves[3] };

                            Move[] player1_set7 = new Move[] { playerMoves[1], playerMoves[2], playerMoves[3] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player1_set2, winning_moves1[i]) ||
                                checkSubset(player1_set4, winning_moves1[i]) ||
                                checkSubset(player1_set7, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        if (moves == 9)
                        {
                            Move[] player1_set3 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[4] };

                            Move[] player1_set5 = new Move[] { playerMoves[0], playerMoves[2], playerMoves[4] };

                            Move[] player1_set6 = new Move[] { playerMoves[0], playerMoves[3], playerMoves[4] };

                            Move[] player1_set8 = new Move[] { playerMoves[1], playerMoves[2], playerMoves[4] };

                            Move[] player1_set9 = new Move[] { playerMoves[1], playerMoves[3], playerMoves[4] };

                            Move[] player1_set10 = new Move[] { playerMoves[2], playerMoves[3], playerMoves[4] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player1_set3, winning_moves1[i]) ||
                                checkSubset(player1_set5, winning_moves1[i]) || checkSubset(player1_set6, winning_moves1[i]) ||
                                checkSubset(player1_set8, winning_moves1[i]) || checkSubset(player1_set9, winning_moves1[i]) ||
                                checkSubset(player1_set10, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        return false;
                    }

                case 2:
                    /******/
                    if (moves < 6)
                    {
                        return false;
                    }
                    else
                    {
                        /*******/
                        if (moves == 6)
                        {
                            Move[] player2_set1 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[2] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player2_set1, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        if (moves == 8)
                        {
                            Move[] player2_set2 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[3] };

                            Move[] player2_set3 = new Move[] { playerMoves[0], playerMoves[2], playerMoves[3] };

                            Move[] player2_set4 = new Move[] { playerMoves[1], playerMoves[2], playerMoves[3] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player2_set2, winning_moves1[i]) || checkSubset(player2_set3, winning_moves1[i]) ||
                                checkSubset(player2_set4, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        return false;
                    }
                default:
                    return false;
            }
        }

    }
}

 

Finally, the main class:

TicTacToe

The class containing the entry point of the program also called the main class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class TicTacToe
    {

        /**
         * @param args the command line arguments
         */
        public static void Main(string[] args)
        {
            // TODO code application logic here

            Game game = new Game();

            Player player1 = new Player();

            player1.setPlayerNum(1);

            player1.setPlayerMark('X');

            player1.setGame(game);

            Player player2 = new Player();

            player2.setPlayerNum(2);

            player2.setPlayerMark('0');

            player2.setGame(game);

            Board board = new Board();

            board.display();

            /*******/

            game.setWhoseTurn(player1);

            game.setGameOver(false);

            bool status = game.isGameOver();

            //

            do
            {
                int movesNow = Game.getMovesTillNow();

                if (movesNow == 9)
                {
                    if (game.getWinner() == null)
                    {
                        Console.WriteLine("TIE!\n");
                        break;
                    }
                }

                int turn = game.getWhoseTurn();

                Console.WriteLine("PLAYER " + turn + "\n");

                int row = 0;
                int column = 0;

                bool invalid = true;

                while (invalid)
                {
                    //code to handle invalid moves. players cannot proceed without valid input
                    try
                    {
                        Console.WriteLine("Enter row: ");

                        string str = Convert.ToString(Console.ReadLine());

                        if (!(str == ""))
                        {

                            row = Convert.ToInt32(str);
                            invalid = false;

                        }
                        else
                        {
                            continue;
                        }
                    }
                    catch (FormatException n)
                    {
                        Console.WriteLine("INVALID ROW!");
                        invalid = true;
                    }
                    /**/
                }

                invalid = true;

                while (invalid)
                {
                    /*code to handle invalid moves. players cannot proceed without valid input*/

                    try
                    {
                        Console.WriteLine("Enter column: ");

                        String str = Convert.ToString(Console.ReadLine());

                        if (!(str == ""))
                        {

                            column = Convert.ToInt32(str);
                            invalid = false;

                        }
                        else
                        {
                            continue;
                        }
                    }
                    catch (FormatException n)
                    {
                        Console.WriteLine("INVALID COLUMN!");
                        invalid = true;
                    }
                    /**/
                }

                /**/

                Move move = new Move();

                move.setRow(row);

                move.setColumn(column);

                switch (turn)
                {
                    case 1:
                        player1.makeMove(board, move);

                        if (game.checkWinningMoves(player1))
                        {
                            game.setWinner(player1);

                            Console.WriteLine("PLAYER 1 WINS!");

                            game.setGameOver(true);
                        }
                        else
                        {
                            if (move.isIsValid())
                            {
                                movesNow = Game.getMovesTillNow();

                                Console.WriteLine("MOVE NUMBER " + movesNow + "\n");

                                game.setWhoseTurn(player2);
                            }
                        }
                        break;
                    case 2:
                        player2.makeMove(board, move);

                        if (game.checkWinningMoves(player2))
                        {
                            game.setWinner(player2);

                            Console.WriteLine("PLAYER 2 WINS!");

                            game.setGameOver(true);
                        }
                        else
                        {
                            if (move.isIsValid())
                            {
                                movesNow = Game.getMovesTillNow();

                                Console.WriteLine("MOVE NUMBER " + movesNow + "\n");

                                game.setWhoseTurn(player1);
                            }
                        }
                        break;
                    default:
                        Console.WriteLine("ERROR!\n");
                        break;
                }

                status = game.isGameOver();
            }
            while (!status);

            Console.Read();

        }

    }
}

 

Copy, Compile, Run and ENJOY!

Hangman Console Game in Java and Microsoft .net

This game is a console game application based on hangman, a popular game worldwide.
There is a text file with a list of movies in it, one per line. When the game begins, the user is shown a movie title with the letters obscured by asterisks or stars.

The user is asked to guess a letter, not any non-alphabetic character. If the letter does not occur in the movie title, the player will be alerted that the guess is wrong. Also, the player will lose a chance out of a maximum of 5 chances. If all chances are gone and the movie title is still not guessed, the game is lost.

If all letters are guessed within 5 chances, the game is won. The player is then asked if he wants to continue playing the game. If yes, the game goes to another round. If not, the game exits.

The roles and functions are divided into 2 classes:

  1. GetMovie: This will fetch a random movie for playing one round of the game.
  2. GamePlay: Handles the playing of the game.

The code listings for these classes are given below. The game has two versions: Java and C#

Java Version

Get Movie Class

/*
 * 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 com.Hangman;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * 
 */
public class GetMovie {
    
    static ArrayList movies = new ArrayList();
    
    static String movie;
    
    static String path;
    
    GetMovie(String pathToFile)
    {
        path = pathToFile;
    }
    
    public static String getRandMovie()
    {
        String randWord = "";
        try{
        
            InputStream istream = new FileInputStream(path); 

            InputStreamReader isr = new InputStreamReader(istream);

            BufferedReader br = new BufferedReader(isr);
            
            String movie;
            
            while((movie = br.readLine()) != null)
            {
                movies.add(movie);
            }
            
            int size = movies.size();
            
            int idx = (new java.util.Random()).nextInt(size);
            
            randWord = movies.get(idx);
        
        }
        catch(FileNotFoundException f)
        {
            System.out.println(f.getMessage());
            
            System.exit(0);
        }
        catch(IOException io)
        {
            System.out.println(io.getMessage());
            
            System.exit(0);
        }
        
        return randWord;
    }
    
}

 

GamePlay Class

/*
 * 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 com.Hangman;

import java.util.Scanner;

/**
 *
 * 
 */
public class GamePlay {
    
    static char[] movieChars;
    static char[] guessChars;
    static int chances = 5;
    
    static void renderMovie(String movie)
    {
        movieChars = movie.toCharArray();
        
        guessChars = new char[movieChars.length];
        
        for(int i = 0; i&lt; movieChars.length; i++)
        {
            if(Character.isLetter(movieChars[i]))
            {
                System.out.print("* ");
            }
            else
            {
                System.out.print(movieChars[i] + " ");
                
                guessChars[i] = movieChars[i];
                
            }
        }
        
        System.out.println();
    }
    
    private static boolean playChar(char ch)
    {
        int matchCount = 0;
        
        for(int i = 0; i &lt; movieChars.length; i++)
        {
            if(Character.toLowerCase(movieChars[i]) == Character.toLowerCase(ch))
            {
                matchCount++;
                
                guessChars[i] = movieChars[i];
            }
        }
        
        if(matchCount == 0)
        {
            chances--;
                
            System.out.println("WRONG Guess! You have "+chances+" chances left!");
        }
        
        /**/
        
        for(int i = 0; i&lt; guessChars.length; i++)
        {
            if(guessChars[i] == '\0')
            {
                System.out.print("* ");
            }
            else
            {
                System.out.print(guessChars[i]+ " ");
            }
        }
        
        System.out.println();
        
        
        String movie = new String(movieChars);
        
        String guessed = new String(guessChars);
        
        return movie.equals(guessed);
        
    }
    
    public static void main(String[] args)
    {
        GetMovie gm = new GetMovie("movies.txt");
        
        char choice = '\0';
        
        System.out.println("/***************WELCOME TO JAVA HANGMAN**************/");
        
        do{
        
            chances = 5;
            
            String movie = GetMovie.getRandMovie();

            renderMovie(movie);

            System.out.print("Enter a letter: ");

            Scanner sc = new Scanner(System.in);

            char gchar = sc.next().charAt(0);

            while(!playChar(gchar))
            {
                if(chances == 0)
                {
                    break;
                }

                System.out.print("Enter a letter: ");

                sc = new Scanner(System.in);

                gchar = sc.next().charAt(0);
            }

            if(chances == 0)
            {
                System.out.println("You LOSE! The movie was: \n"+movie);
            }
            else
            {
                System.out.println("You WIN!");
            }
            
            System.out.print("\nPlay again (y/n)?: ");

            sc = new Scanner(System.in);

            choice = sc.next().charAt(0);
        
        }
        while(choice == 'y' || choice == 'Y');
        
        System.out.println("EXITING...");
        System.exit(0);
    }
    
}

 

Copy, compile, run and ENJOY!

C# Version

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleHangman
{
    class GetMovie
    {
        private static List lines = new List();

        static string movie;

        static string path;

        public GetMovie(string pathToFile)
        {
            path = pathToFile;
        }

        internal static string getRandMovie()
        {
            System.IO.StreamReader file =
            new System.IO.StreamReader(@path);

            string line;

            while ((line = file.ReadLine()) != null)
            {
                lines.Add(line);
            }

            int count = lines.Count;

            Random r = new Random();

            int index = r.Next(count);

            string line1 = lines[index];

            file.Close();

            return line1; //working
        }
    }

    class GamePlay
    {
        static char[] movieChars;
        static char[] guessChars;
        static int chances = 5;

        static void renderMovie(String movie)
        {
            movieChars = movie.ToCharArray();

            guessChars = new char[movieChars.Length];

            for (int i = 0; i &lt; movieChars.Length; i++)
            {
                if (Char.IsLetter(movieChars[i]))
                {
                    Console.Write("* ");
                }
                else
                {
                    Console.Write(movieChars[i] + " ");

                    guessChars[i] = movieChars[i];

                }
            }

            Console.WriteLine();
        }

        private static bool playChar(char ch)
        {
            int matchCount = 0;

            for (int i = 0; i &lt; movieChars.Length; i++)
            {
                if (Char.ToLower(movieChars[i]) == Char.ToLower(ch))
                {
                    matchCount++;

                    guessChars[i] = movieChars[i];
                }
            }

            if (matchCount == 0)
            {
                chances--;

                Console.WriteLine("WRONG Guess! You have " + chances + " chances left!");
            }

            /**/

            for (int i = 0; i &lt; guessChars.Length; i++)
            {
                if (guessChars[i] == '\0')
                {
                    Console.Write("* ");
                }
                else
                {
                    Console.Write(guessChars[i] + " ");
                }
            }

            Console.WriteLine();


            string movie = new string(movieChars);

            string guessed = new string(guessChars);

            return (movie == guessed);

        }

        public static void Main(string[] args)
        {
            GetMovie gm = new GetMovie("..\\..\\movies.txt");

            char choice = '\0';

            Console.WriteLine("/***************WELCOME TO C# HANGMAN**************/");

            do
            {

                chances = 5;

                String movie = GetMovie.getRandMovie();

                renderMovie(movie);

                Console.Write("Enter a letter: ");

                char gchar = Convert.ToChar(Console.ReadLine());

                while (!playChar(gchar))
                {
                    if (chances == 0)
                    {
                        break;
                    }

                    Console.Write("Enter a letter: ");

                    gchar = Convert.ToChar(Console.ReadLine());
                }

                if (chances == 0)
                {
                    Console.WriteLine("You LOSE! The movie was: \n" + movie);
                }
                else
                {
                    Console.WriteLine("You WIN!");
                }

                Console.Write("\nPlay again (y/n)?: ");

                choice = Convert.ToChar(Console.ReadLine());

            }
            while (choice == 'y' || choice == 'Y');

            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();
        }
    }
}

 

See the video below. COPY, COMPILE, RUN and ENJOY!

Cows and Bulls: A simple word guessing game

“Cows and Bulls” is a simple word guessing game. In the beginning, the player is told how many letters there are in the word to guess. This is indicated by the number of stars or asterisks displayed. Then the player guesses the word. If the guess is correct, the player is declared a winner and is then prompted to continue or end the game.

If not, the player is told how many hits or “bulls” he made and how many misses or “cows” he made. The player is also asked if he wants to give up.If not, he must continue playing. If he gives up, he will be told what the word was. He will then be prompted to continue or end the game.

One of the subroutines in this program retrieves a random word from a dictionary file which is essentially a text file containing one word per line. It stores all the words in a list and then picks a random element based on the size of the list.

This game has 2 versions: Java and C#

Java Version

/*
 * 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 com.cowsandbulls;

import java.io.*;
import java.util.*;
/**
 *
 * @author Dell
 */
public class CowsAndBulls {
    
    static ArrayList words = new ArrayList();
    
    private static String getRandWord(String path)
    {
        String randWord = "";
        
        try{
        
            InputStream istream = new FileInputStream("words.txt"); 

            InputStreamReader isr = new InputStreamReader(istream);

            BufferedReader br = new BufferedReader(isr);
            
            String word;
            
            while((word = br.readLine()) != null)
            {
                words.add(word);
            }
            
            int size = words.size();
            
            int idx = (new java.util.Random()).nextInt(size);
            
            randWord = words.get(idx);
        
        }
        catch(FileNotFoundException f)
        {
            System.out.println(f.getMessage());
            
            System.exit(0);
        }
        catch(IOException io)
        {
            System.out.println(io.getMessage());
            
            System.exit(0);
        }
        
        return randWord;
    }
    
    public static void main(String[] args)
    {
        char cont = '\0';
        
        Scanner sc  = new Scanner(System.in);
        
        System.out.println("WELCOME TO COWS AND BULLS!\n");
        
        do{
        
            String w = getRandWord("words.txt"); //ok
            //System.out.println(w);

            char[] guessChars = w.toCharArray();

            char giveUp = '\0';

            

            for(int i = 0; i&lt; w.length(); i++)
            {
                System.out.print("* ");
            }

            System.out.println("\n");

            do
            {

                System.out.print("Guess the word: ");

                String guess = sc.nextLine();

                if(guess.equals(w))
                {
                    System.out.println("YOU WIN!\n");

                    break;
                }
                else
                {

                    if(guess.length() == w.length())
                    {
                        int cows = 0, bulls = 0;

                        for(int i = 0;i &lt; w.length(); i++)
                        {
                            if(w.charAt(i) == guess.charAt(i))
                            {
                                bulls++;

                                System.out.print("bull ");
                            }
                            else
                            {
                                cows++;

                                System.out.print("cow ");
                            }
                        }

                        System.out.println();

                        System.out.println(bulls + " bulls, "+cows+" cows.\n");
                    }
                    else
                    {
                        System.out.println("LENGTH MISMATCH!");
                    }

                    
                    System.out.println("Do you give up? (y/n): ");
                    
                    String s = sc.nextLine();
                    
                    while(s.length() == 0)
                    {
                        System.out.println("Do you give up? (y/n): ");
                        s = sc.nextLine();
                    }
                    
                    giveUp = s.charAt(0);
                    
                    //giveUp = sc.nextLine().charAt(0);


                }
            }
            while(giveUp != 'y' &amp;&amp; giveUp != 'Y');

            if(giveUp == 'y' || giveUp == 'Y')
            {
                System.out.println("The word was: "+w);
            }


            System.out.println("Continue? (y/n): ");
            
            String s = sc.nextLine();
                    
            while(s.length() == 0)
            {
                System.out.println("Continue? (y/n): ");
                s = sc.nextLine();
            }

            //
            
            cont = s.charAt(0);
        
        
        }while(cont == 'y' || cont == 'Y');
        
        System.out.println("Press any key to exit...");
        
        /* */
    System.console().readLine();
            
    System.exit(0);
        
    }
    
}

 

Copy, compile, run and ENJOY!

C# Version

The code is in the listing.
Also, see the video below.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CowsAndBulls
{
    class Program
    {

        private static List lines = new List();

        private static string GetWordFromFile(string path)
        {
            System.IO.StreamReader file =
            new System.IO.StreamReader(@path);

            string line;

            while ((line = file.ReadLine()) != null)
            {
                lines.Add(line);
            }

            int count = lines.Count;

            Random r = new Random();

            int index = r.Next(count);

            string line1 = lines[index];

            file.Close();

            return line1; //working
        }

        static void Main(string[] args)
        {
            //

            char cont = '\0';

            Console.WriteLine("WELCOME TO COWS AND BULLS!\n");

            do
            {

                string w = GetWordFromFile("..\\..\\words.txt"); //ok
                                                                 

                char[] guessChars = w.ToCharArray();

                char giveUp = '\0';

                //

                for (int i = 0; i &lt; w.Length; i++)
                {
                   Console.Write("* ");
                }

                Console.WriteLine("\n");

                do
                {

                    Console.Write("Guess the word: ");

                    string guess = Console.ReadLine();

                    if (guess == w)
                    {
                        Console.WriteLine("YOU WIN!\n");

                        break;
                    }
                    else
                    {

                        if (guess.Length == w.Length)
                        {
                            int cows = 0, bulls = 0;

                            for (int i = 0; i &lt; w.Length; i++)
                            {
                                if (w.Substring(i, 1) == guess.Substring(i, 1))
                                {
                                    bulls++;

                                    Console.Write("bull ");
                                }
                                else
                                {
                                    cows++;

                                    Console.Write("cow ");
                                }
                            }

                            Console.WriteLine();

                            Console.WriteLine(bulls + " bulls, " + cows + " cows.\n");
                        }
                        else
                        {
                            Console.WriteLine("LENGTH MISMATCH!");
                        }


                        Console.WriteLine("Do you give up? (y/n): ");

                        string s = Console.ReadLine();

                        while (s.Length == 0)
                        {
                            Console.WriteLine("Do you give up? (y/n): ");
                            s = Console.ReadLine();
                        }

                        giveUp = Convert.ToChar(s.Substring(0, 1));

                        //


                    }
                }
                while (giveUp != 'y' &amp;&amp; giveUp != 'Y');

                if (giveUp == 'y' || giveUp == 'Y')
                {
                    Console.WriteLine("The word was: " + w);
                }


                Console.WriteLine("Continue? (y/n): ");

                string s1 = Console.ReadLine();

                while (s1.Length == 0)
                {
                    Console.WriteLine("Continue? (y/n): ");
                    s1 = Console.ReadLine();
                }

                //

                cont = Convert.ToChar(s1.Substring(0, 1));


            } while (cont == 'y' || cont == 'Y');

            Console.WriteLine("Press any key to exit...");

            Console.Read();

        }
    }
}

 

Copy, compile, run and ENJOY!