April 28, 2020

Create a simple Tree data structure in Java from an array of values

Tree is a data Structure to create a hierarchical structure


Creating a Node class



/**
 * Node in Tree
 */
package com.cfed.datastructures;

/**
 * @author Konzernites
 *
 */
public class Node {

private int data;
private Node left;
private Node right;

public Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}

public String toString() {
return "data is "+data;
}

public int getData() {
return data;
}

public void setData(int data) {
this.data = data;
}

public Node getLeft() {
return left;
}

public void setLeft(Node left) {
this.left = left;
}

public Node getRight() {
return right;
}

public void setRight(Node right) {
this.right = right;
}
}


Adding  values to a tree



/**
 * Adding values to tree
 */
package com.cfed.datastructures;

/**
 * @author Konzernites
 *
 */
public class BinaryTree {

private Node root;

private Node addRecursion(Node current, int data) {
if (null == current) {
return new Node(data);
} else if( null == current.getLeft()) {
current.setLeft(addRecursion(current.getLeft(), data));
}else if(null == current.getRight()) {
current.setRight(addRecursion(current.getRight(), data));
}else {
addRecursion(current.getLeft(), data);
}
return current;
}

public void addData(int value) {
root = addRecursion(root, value);
}
}


Mainclass.java the entry class


/**
 *  Entry point of the program
 */
package com.cfed.datastructures;

/**
 * @author Athul Ramesh
 *
 */
public class TreeTesting {

public static void main(String[] args) {
int arr[] = {8,6,7,4,3,1,2};
BinaryTree binary = new BinaryTree();
for(int i =0; i< arr.length; i++) {
binary.addData(arr[i]);
}
}
}


You can use this program to store data in a hierarchical structure.

April 21, 2020

Making Tic Tac Toe using abstract and interface in Java Full source code


What we did here is, using the OOPs concepts abstract and interface we change the face of the project
Help us to do simple modification in future so as to add new games using the same matrix.
You have to go through the previous tutorials to fully understand the concepts.


Change in Tic Tac Toe class

public class TicTacToe implements IGame

Changes in Main class


import com.cfed.matrix.tictactoe.IGame;

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright (c) 1997-2017 Javabelazy and/or its affiliates. All rights reserved.
 *
 */

/**
 * @author konzernites
 * @since 1.0
 *
 */
public class Main extends AbstractGame {

private static final long serialVersionUID = 8683452581122892182L;

/**
* @param args
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IllegalAccessException {

Main mainClass = new Main();
mainClass.execute();

}

private void execute() throws IllegalAccessException {
Matrix<Character> matrix = new Matrix<>(3, 3);

System.out.println(" **** INSTRUCTIONS **** ");
System.out.println(" User has to enter row and column ");
System.out.println(" Postion starts from (0,0) to (2,2) ");

IGame game = new TicTacToe(matrix);
TicTacToe.isTwoPlayer = true;
playGame(game);

System.out.println(" ***** GAME OVER ***** ");
System.out.println(" Developed by consumerfed I T Section ");

}

}

AbstractGame.java


/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright (c) 1997-2017 Javabelazy and/or its affiliates. All rights reserved.
 *
 */

import com.cfed.matrix.tictactoe.IGame;

/**
 *
 */

/**
 * @author Konzernites
 *
 */
public abstract class AbstractGame {

public void playGame(IGame game) throws IllegalAccessException {
game.startGame();
}

}


IGame Interface in java


/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright (c) 1997-2017 Javabelazy and/or its affiliates. All rights reserved.
 *
 */
/**
 * 
 */
package com.cfed.matrix.tictactoe;

/**
 * @author jwalian walla bag
 *
 */
public interface IGame {
public void startGame() throws IllegalAccessException;

}





April 14, 2020

How to make the Tic Tac Toe Game a two player | Java source code

We have already developed a code for Tic Tac Toe Game. For making the code to a two player game, we need to make one simple modification in TicTacToe.Java file

Create a new method

private void getUserMove(int rowMoved, int colMoved, char currentMove) throws IllegalAccessException {
Scanner scanner = null;
scanner = new Scanner(System.in);
System.out.println(" *** THE USER :  " + currentMove + " MOVE *** ");
System.out.println(" Enter the row (values from 0 to 2): ");
int row = scanner.nextInt();
validate(row);
System.out.println(" Enter the col (values from 0 to 2): ");
int col = scanner.nextInt();
validate(col);
matrix.add(row, col, currentMove);
rowMoved = row;
colMoved = col;
}


Change the following method


@SuppressWarnings("resource")
public void startGame() throws IllegalAccessException {

int rowMoved = 0;
int colMoved = 0;

while (moveCount < MAX_MOVE) {

if (moveCount % 2 == 0) {

if (isTwoPlayer) {
getUserMove(rowMoved, colMoved, COMP_MOVE);
} else {
matrix = computerMove.computerMove(rowMoved, colMoved);
System.out.println("** COMPUTER MOVE **");
try {
Thread.sleep(3121);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

} else {
getUserMove(rowMoved, colMoved, USER_MOVE);
}
matrix.print();
boolean isWon = checkWinner();
if (isWon) {
String name = winner.equals(TikTocToe.USER_MOVE) ? " USER " : " COMPUTER ";
System.out.println("*** Congratulation *** ");
System.out.println(name + " is the winner ");
break;
}
moveCount++;
}

}


Create a member variable - isTwoPlayer (boolean)


public static boolean isTwoPlayer = false;


In MainClass, the user has to choose whether you prefer a two player game or  you like to play with computer...

To see the full source code, check our previous posts.






April 07, 2020

Tic Tac Toe - Play with computer | Full Java source code

ComputerMove.java

You will get a null pointer exception in tic tac toe game, for the java code which we posted earlier.
So these changes has to be made in the code.
While the user chose the first move other than the centre position, the Nullpointer Exception comes.

private Matrix<Character> generateFirstCompMove() throws IllegalAccessException {
int row = 1;
int col = 1;

if(matrix.isElementExist(row, col)) {
Random r = new Random();
row = r.nextInt(3);
col = r.nextInt(3);
col = (row == 1 && col == 1) ? col + 1 : col;
matrix.add(row, col, TikTocToe.COMP_MOVE);

}else {
matrix.add(1, 1, TikTocToe.COMP_MOVE);
}

return matrix;
}

Facebook comments