January 28, 2020

Sorting two arrays into one array in efficient way

Sorting two sorted square number array to single array

/**
 * inputs :
 * int[] a = { 100, 64, 36, 2 };
int[] b = { 1, 9, 36, 49, 81 };

output :

[1, 2, 9, 36, 36, 49, 64, 81, 100]

 */
package com.codecreeks.solutions;

import java.util.Arrays;

/**
 * @author Athul
 *
 */
public class SortingAlgorithm {

/**
* @param javacode
*/
public static void main(String[] javacode) {

int[] a = { 100, 64, 36, 2 };
int[] b = { 1, 9, 36, 49, 81 };

SortingAlgorithm algo = new SortingAlgorithm();
int[] output = algo.sort(a, b);
System.out.println(Arrays.toString(output));

}

private int[] sort(int[] a, int[] b) {
int bLen = b.length;
int aLen = a.length;
int len = aLen + bLen;
int aPtr = aLen - 1;
int bPtr = 0;

int[] output = new int[len];

for (int i = 0; i < len; i++) {

if (bPtr > (bLen - 1)) {
while (aPtr >= 0) {
output[i] = a[aPtr];
i++;
aPtr--;
}
break;
}

if (aPtr < 0) {
while (bPtr < bLen) {
output[i] = b[bPtr];
i++;
bPtr++;
}
break;
}

if (b[bPtr] < a[aPtr]) {
output[i] = b[bPtr];
bPtr++;
} else {
output[i] = a[aPtr];
aPtr--;
}

}

return output;
}

}



One of the biggest mistakes we all make is thinking we need more time than we actually do.

- Admond Lee Kin Lim


January 21, 2020

Tic Tac Toe in Java - The Matrix code

Tic Tac Toe algorithm and source code 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.tiktactoe;

import java.util.NoSuchElementException;

/**
 * @author konzernites
 * @since 1.0
 * id : 6523895
 */
public class Matrix<E> extends AbstractMatrix<E> {

@SuppressWarnings("unused")
private static final long serialVersionUID = 8683452581122892189L;

private final int MAX_COLUMN;
private final int MAX_ROW;
private final int MAX_CAPACITY;
protected static final char EMPTY_DATA = '\0';
private static boolean isEditable = false;

private E elementData[][];
private int capacity = 0;

public Matrix(int row, int column) {
this.MAX_COLUMN = column;
this.MAX_ROW = row;
this.MAX_CAPACITY = MAX_ROW * MAX_COLUMN;
intialize();
}

@SuppressWarnings("unchecked")
private void intialize() {
this.elementData = (E[][]) new Object[MAX_ROW][MAX_COLUMN];
}

public Matrix() {
this(2, 2);
}

public void add(int row, int column, E value) throws IllegalAccessException {
if (row > MAX_ROW || column > MAX_COLUMN)
throw new IllegalAccessException("Illegal capacity");
else if (isFull() || isElementExist(row, column))
throw new IllegalAccessException("Value already exist");
else {
capacity++;
this.elementData[row][column] = value;
}
}

public E[][] getMatrix() {
if (isEmpty())
throw new NoSuchElementException("Matrix is empty");
else
return this.elementData;
}

public E getValue(int row, int column) {
if (isEmpty())
return null;
else if(isElementExist(row, column))
return this.elementData[row][column];
else
return null;
}

public void remove(int row, int column) {
if (isEmpty())
throw new NoSuchElementException("Cannot remove value from an empty matrix");
else {
this.elementData[row][column] = null;
capacity--;
}
}

public void removeAll() {
if (isEmpty())
throw new NoSuchElementException("Cannot remove value from an empty matrix");
else {
// TODO remove all elements from matrix
}
}

public void print() {
if (isEmpty())
throw new NoSuchElementException("Cannot display an empty matrix");
else {
for (int r = 0; r < MAX_ROW; r++) {
for (int c = 0; c < MAX_COLUMN; c++) {
if (null == elementData[r][c])
System.out.print("  | ");
else
System.out.print(elementData[r][c] + " | ");
}
System.out.println("");
}
}
}

public Dimensions dimension() {
Dimensions dimensions = new Dimensions();
dimensions.setRow(MAX_ROW);
dimensions.setColumn(MAX_COLUMN);
return dimensions;
}

private boolean isEmpty() {
if (capacity == 0)
return true;
else
return false;
}

public boolean isElementExist(int row, int column) {
if (isEmpty())
return false;
else if (null != elementData[row][column])
return true;
else
return false;
}

private boolean isFull() {
if (capacity >= MAX_CAPACITY)
return true;
else
return false;
}

public int size() {
return capacity;
}

}

An abstract Matrix for Tic Tac Toe.

Tic Tac Toe complete code will be available soon, stay update


January 14, 2020

Tik Tac Toe Game with computer java code games


Tic Tac Toe algorithm and source code in Java


Developed a computer algorithm that play with user in Tik Tac Toe Game..




User Move and Computer moves are distinguished by characters X and O ( upper case)



** YOUR MOVE **
 Enter the row:
0
 Enter the col:
0
X |   |   |
  |   |   |
  |   |   |
** COMPUTER MOVE **
X |   |   |
  | O |   |
  |   |   |
** YOUR MOVE **
 Enter the row:
0
 Enter the col:
1
X | X |   |
  | O |   |
  |   |   |
** COMPUTER MOVE **
X | X | O |
  | O |   |
  |   |   |
** YOUR MOVE **
 Enter the row:
2
 Enter the col:
0
X | X | O |
  | O |   |
X |   |   |
** COMPUTER MOVE **
X | X | O |
O | O |   |
X |   |   |
** YOUR MOVE **
 Enter the row:
2
 Enter the col:
2
X | X | O |
O | O |   |
X |   | X |
** COMPUTER MOVE **
X | X | O |
O | O |   |
X | O | X |

 Developed by consumerfed I T section kozhikode ( 26608629 )

Previous >> Tic Tac Toe prototype developed
Next >> Tic Tac Toe the matrix code

Complete source code will be posted soon (38064021)

Buy any amazon product from here


January 07, 2020

Tic Toc Toe Prototype developed in java

Developing Games using Java Programming

package com.konzern.games;

import java.util.Scanner;

/**
 *  Tic Toc Toe Prototype developed in java
 *  Thanks to Steffi Thomas
 */

/**
 * @author consumerfed IT Section
 *
 */
public class TikTokToe {

private int MATRIX_SIZE = 3;
private char[][] matrix = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
private int moveCount = 0;

public TikTokToe() {
intialize();
}

private void intialize() {
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] matrix = { { 'x', 'o', 'x' }, { 'x', ' ', ' ' }, { 'o', 'x', 'x' } };
TikTokToe t = new TikTokToe();
t.startGame();
// boolean isWin = t.checkWinner(matrix, 'x');
// t.printMatrix();
System.out.println(" Developed by consumerfed I T section kozhikode ");

}

private void startGame() {
// TODO Auto-generated method stub
char move = 'x';
Scanner scanner = new Scanner(System.in);
while (moveCount < 9) {
System.out.println("** MOVE FOR " + move + " **");
System.out.println(" Enter the row:");
int row = scanner.nextInt();
System.out.println(" Enter the col:");
int col = scanner.nextInt();
matrix[row][col] = move;
printMatrix();
boolean winner = checkWinner(matrix, move);
if (winner) {
System.out.println("*** Congratulation *** ");
System.out.println(move + " is the winner ");
break;
}
move = ('x' == move) ? 'o' : 'x';
}

}

private void printMatrix() {
// TODO Auto-generated method stub

for (int row = 0; row < MATRIX_SIZE; row++) {
for (int col = 0; col < MATRIX_SIZE; col++) {
System.out.print(matrix[row][col] + " | ");
}
System.out.println("");
}

}

private void checkWinner() {
// TODO Auto-generated method stub

}

private boolean checkWinner(char[][] matrix, char move) {

if (move == matrix[0][0] && move == matrix[0][1] && move == matrix[0][2]) {
return true;
} else if (move == matrix[1][0] && move == matrix[1][1] && move == matrix[1][2]) {
return true;
} else if (move == matrix[2][0] && move == matrix[2][1] && move == matrix[2][2]) {
return true;
} else if (move == matrix[0][0] && move == matrix[1][0] && move == matrix[2][0]) {
return true;
} else if (move == matrix[0][1] && move == matrix[1][1] && move == matrix[2][1]) {
return true;
} else if (move == matrix[0][2] && move == matrix[1][2] && move == matrix[2][2]) {
return true;
} else if (move == matrix[0][0] && move == matrix[1][1] && move == matrix[2][2]) {
return true;
} else if (move == matrix[0][2] && move == matrix[1][1] && move == matrix[2][0]) {
return true;
} else {
return false;
}
// return false;
}
}


Output


** MOVE FOR x **
 Enter the row:
0
 Enter the col:
0
x |   |   | 
  |   |   | 
  |   |   | 
** MOVE FOR o **
 Enter the row:
1
 Enter the col:
1
x |   |   | 
  | o |   | 
  |   |   | 
** MOVE FOR x **
 Enter the row:
1
 Enter the col:
0
x |   |   | 
x | o |   | 
  |   |   | 
** MOVE FOR o **
 Enter the row:
2
 Enter the col:
2
x |   |   | 
x | o |   | 
  |   | o | 
** MOVE FOR x **
 Enter the row:
2
 Enter the col:
0
x |   |   | 
x | o |   | 
x |   | o | 


x is the winner 

Suggested Books


Facebook comments