tic tac toe code

import java.util.Scanner;
import java.util.Random;

public class TicTacToe{

public static void main(String[] args){
new TicTacToe();
}

char[][]board = new char[3][3];
char player;

char cpu;

char empty;

public TicTacToe(){
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
board[i][j] = ‘ ‘;
}
}

player = ‘X’;

cpu = ‘O’;

empty = ‘ ‘;

Scanner user_input = new Scanner(System.in);

System.out.println(”   0   1   2″);
System.out.println(“0″ + ”  ” + board[0][0] + ” | ” + board[0][1] + ” | ” + board[0][2]);
System.out.println(”  ———–“);
System.out.println(“1″ + ”  ” + board[1][0] + ” | ” + board[1][1] + ” | ” + board[1][2]);
System.out.println(”  ———–“);
System.out.println(“2″ + ”  ” + board[2][0] + ” | ” + board[2][1] + ” | ” + board[2][2]);

String a;
System.out.print(“Enter row: (0-2)”);
a = user_input.next();

String b;
System.out.print(“Enter column: (0-2)”);
b = user_input.next();

int x = Integer.parseInt(a);
int y = Integer.parseInt(b);

board[x][y] = player;
System.out.println(”   0   1   2″);
System.out.println(“0″ + ”  ” + board[0][0] + ” | ” + board[0][1] + ” | ” + board[0][2]);
System.out.println(”  ———–“);
System.out.println(“1″ + ”  ” + board[1][0] + ” | ” + board[1][1] + ” | ” + board[1][2]);
System.out.println(”  ———–“);
System.out.println(“2″ + ”  ” + board[2][0] + ” | ” + board[2][1] + ” | ” + board[2][2]);

int o = x -1;
if(x == 0){
o = x +1;
}
int p = y – 0;
if(y==0){
p = y + 1;
}

board[o][p] = cpu;
System.out.println(”   0   1   2″);
System.out.println(“0″ + ”  ” + board[0][0] + ” | ” + board[0][1] + ” | ” + board[0][2]);
System.out.println(”  ———–“);System.out.println(“1″ + ”  ” + board[1][0] + ” | ” + board[1][1] + ” | ” + board[1][2]);
System.out.println(”  ———–“);
System.out.println(“2″ + ”  ” + board[2][0] + ” | ” + board[2][1] + ” | ” + board[2][2]);

System.out.print(“Enter row: (0-2)”);
a = user_input.next();
System.out.print(“Enter column: (0-2)”);
b = user_input.next();

x = Integer.parseInt(a);
y = Integer.parseInt(b);

if(board[x][y] == empty){
board[x][y] = player;
System.out.println(”   0   1   2″);
System.out.println(“0″ + ”  ” + board[0][0] + ” | ” + board[0][1] + ” | ” + board[0][2]);
System.out.println(”  ———–“);
System.out.println(“1″ + ”  ” + board[1][0] + ” | ” + board[1][1] + ” | ” + board[1][2]);
System.out.println(”  ———–“);
System.out.println(“2″ + ”  ” + board[2][0] + ” | ” + board[2][1] + ” | ” + board[2][2]);
}

o = x – 0;
if(x == 0){
o = x + 2;
}
p = y -1;
if(y==0){
p = y+1;
}
if(board[o][p] == empty){
board[o][p] = cpu;
System.out.println(”   0   1   2″);
System.out.println(“0″ + ”  ” + board[0][0] + ” | ” + board[0][1] + ” | ” + board[0][2]);
System.out.println(”  ———–“);System.out.println(“1″ + ”  ” + board[1][0] + ” | ” + board[1][1] + ” | ” + board[1][2]);
System.out.println(”  ———–“);
System.out.println(“2″ + ”  ” + board[2][0] + ” | ” + board[2][1] + ” | ” + board[2][2]);
}

System.out.print(“Enter row: (0-2)”);
a = user_input.next();
System.out.print(“Enter column: (0-2)”);
b = user_input.next();

x = Integer.parseInt(a);
y = Integer.parseInt(b);

if(board[x][y] == empty){
board[x][y] = player;
System.out.println(”   0   1   2″);
System.out.println(“0″ + ”  ” + board[0][0] + ” | ” + board[0][1] + ” | ” + board[0][2]);
System.out.println(”  ———–“);
System.out.println(“1″ + ”  ” + board[1][0] + ” | ” + board[1][1] + ” | ” + board[1][2]);
System.out.println(”  ———–“);
System.out.println(“2″ + ”  ” + board[2][0] + ” | ” + board[2][1] + ” | ” + board[2][2]);
}

o = x – 1;
if(x == 0){
o = x + 1;
}
p = y – 0;
if(y==0){
p = y + 2;
}
if(board[o][p] == empty){
board[o][p] = cpu;
System.out.println(”   0   1   2″);
System.out.println(“0″ + ”  ” + board[0][0] + ” | ” + board[0][1] + ” | ” + board[0][2]);
System.out.println(”  ———–“);System.out.println(“1″ + ”  ” + board[1][0] + ” | ” + board[1][1] + ” | ” + board[1][2]);
System.out.println(”  ———–“);
System.out.println(“2″ + ”  ” + board[2][0] + ” | ” + board[2][1] + ” | ” + board[2][2]);
}
}
}

week 7

Today was the last time I could work on the project…. and it is only part done, I’m quite happy with how the player looks, but the AI is kind of awkward… perhaps I’ll fine tune it before the deadline. The game itself isn’t written in the most compact and clean way, but it is in working condition and does not look too bad.

week 6

Seems I was far too adventurous with my ideas for what I would accomplish for this project… I  have learned quite a bit of the java language, but I haven’t been able to create much that actually represents the learning well… hopefully I’ll create the tic tac toe game in time…… I’ve been able to make the board quite easily, but the player and AI is quite difficult for some reason

week 5

set up java on my computer….. was fun….. so I can kind of work without a compiler if I must

decided that making a maze is beyond my skill level. Instead I will make a tic tac toe game. The board seems quite simple to make, as well as the general code for the player.

week 4

Went through Oracle Java documentation, and wrote a few random programs.

 

Prints out random names from dictionary … uses two arrays

class Animals {
public static void main(String[] args) {
String[][] name = {
{“Pink “,”Orange “,”Red “,”Blue “},
{“Cat”,”Lizard”,”Dog”,”Giraffe”}
}

System.out.println(name[0][2] + name[1][1]);
}
}

a calculator … number 1 + number 2 * number 3 … with results going up to 20000

import java.util.Scanner;

public class calc{
public static void main(String[] args){

Scanner user_input = new Scanner(System.in);

String a;
System.out.print(“Enter the first number “);
a = user_input.next();

String b;
System.out.print(“Enter the second number “);
b = user_input.next();

String c;
System.out.print(“Enter the third number “);
c = user_input.next();

int x = Integer.parseInt(a);
int y = Integer.parseInt(b);
int z = Integer.parseInt(c);
int num = x + y;

while (num < 20000){
num = num * z;
System.out.println(num);
}

}
}