Alek Modi

Alek Modi
Follow You Heart!

Sunday, July 22, 2012

Tic - Tac - Toe Source Code in JAVA


import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class tictac {

static int turn = 0;
static int board[][] = {{0,0,0},{0,0,0},{0,0,0}};
static ImageIcon blank = new ImageIcon("res/blank.png");
static JButton button[][] = new JButton[3][3];
static ImageIcon opic = new ImageIcon("res/o.png");
static ImageIcon xpic = new ImageIcon("res/x.png");

public static void main(String[] args) {
// Alek Modi
// 3/22/11
// Tic Tac Toe board
// Import custom images
//USE YOUR OWN IMAGES HERE if needed.
ImageIcon turtle = new ImageIcon("res/turtle.png");
ImageIcon jiraffe = new ImageIcon("res/jiraffe.png");
ImageIcon elephant = new ImageIcon("res/elephant.png");
ImageIcon frog = new ImageIcon("res/frog.png");
ImageIcon hippo = new ImageIcon("res/hippo.png");
ImageIcon owl = new ImageIcon("res/owl.png");
ImageIcon penguin = new ImageIcon("res/penguin.png");
ImageIcon seal = new ImageIcon("res/seal.png");

String promt1 = "Choose your icon:\n" + "1 - Turtle\n" + "2 - Giraffe\n" + "3 - Elephant\n" + "4 - Frog";
String promt2 = "Choose your icon:\n" + "1 - Hippo\n" + "2 - Owl\n" + "3 - Penguin\n" + "4 - Seal";

// Enter Player's names and choose pictures
final String player1 = JOptionPane.showInputDialog("Enter Player 1's name:");
if (player1==null || player1.equals(""))
System.exit(0);
String entry1 = JOptionPane.showInputDialog(player1 + "\n" + promt1);
if (entry1.equalsIgnoreCase("1") | entry1.equalsIgnoreCase("turtle"))
opic = turtle;
else if (entry1.equalsIgnoreCase("2") | entry1.equalsIgnoreCase("giraffe"))
opic = jiraffe;
else if (entry1.equalsIgnoreCase("3") | entry1.equalsIgnoreCase("elephant"))
opic = elephant;
else if (entry1.equalsIgnoreCase("4") | entry1.equalsIgnoreCase("frog"))
opic = frog;
else
JOptionPane.showMessageDialog(null," Incorrect pick.\n" + "You'll be using O's");

final String player2 = JOptionPane.showInputDialog("Enter Player 2's name:");
if (player2==null || player2.equals(""))
System.exit(0);
String entry2 = JOptionPane.showInputDialog(player2 + "\n" + promt2);
if (entry2.equalsIgnoreCase("1") | entry2.equalsIgnoreCase("hippo"))
xpic = hippo;
else if (entry2.equalsIgnoreCase("2") | entry2.equalsIgnoreCase("owl"))
xpic = owl;
else if (entry2.equalsIgnoreCase("3") | entry2.equalsIgnoreCase("penguin"))
xpic = penguin;
else if (entry2.equalsIgnoreCase("4") | entry2.equalsIgnoreCase("seal"))
xpic = seal;
else
JOptionPane.showMessageDialog(null," Incorrect pick.\n" + "You'll be using X's");

// Top image
BufferedImage bi = null;
try {
File imagefile = new File("res/name.png");
bi = ImageIO.read(imagefile);
}
catch (IOException e){
System.out.println("cannot open file");
System.exit(0);
}

// Main frame construction
JFrame myframe = new JFrame ("Tic Tac Toe");

// Set grid layout for board with buttons and images
//ImageIcon blank = new ImageIcon("./blank.png");

Container box = myframe.getContentPane();
box.setLayout(new GridLayout(3,3));

for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
button[i][j] = new JButton(blank);
box.add(button[i][j]);
button[i][j].setBackground(Color.white);
button[i][j].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JButton jb = (JButton)arg0.getSource();
int row = -1;
int col = -1;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (jb == button[i][j]) {
row = i;
col = j;
}
}
}
if (board[row][col] == 0) {

// Player 1
if (turn % 2 == 0){
jb.setIcon(opic);
turn++;
board[row][col]=1;

//Message announcing winner in row
if (board[0][0] + board[0][1] + board[0][2] == 3){
button[0][0].setBackground(Color.red);
button[0][1].setBackground(Color.red);
button[0][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[1][0] + board[1][1] + board[1][2] == 3){
button[1][0].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[1][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[2][0] + board[2][1] + board[2][2] == 3){
button[2][0].setBackground(Color.red);
button[2][1].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

//Message announcing winner in column
if (board[0][0] + board[1][0] + board[2][0] == 3){
button[0][0].setBackground(Color.red);
button[1][0].setBackground(Color.red);
button[2][0].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][1] + board[1][1] + board[2][1] == 3){
button[0][1].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][1].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][2] + board[1][2] + board[2][2] == 3){
button[0][2].setBackground(Color.red);
button[1][2].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

//Message announcing winner in verticals
if (board[0][0] + board[1][1] + board[2][2] == 3){
button[0][0].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][2] + board[1][1] + board[2][0] == 3){
button[0][2].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][0].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}
}

// Player 2
else{
jb.setIcon(xpic);
turn++;
board[row][col]=-1;

//Message announcing winner in row
if (board[0][0] + board[0][1] + board[0][2] == -3){
button[0][0].setBackground(Color.red);
button[0][1].setBackground(Color.red);
button[0][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[1][0] + board[1][1] + board[1][2] == -3){
button[1][0].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[1][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[2][0] + board[2][1] + board[2][2] == -3){
button[2][0].setBackground(Color.red);
button[2][1].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

//Message announcing winner in column
if (board[0][0] + board[1][0] + board[2][0] == -3){
button[0][0].setBackground(Color.red);
button[1][0].setBackground(Color.red);
button[2][0].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][1] + board[1][1] + board[2][1] == -3){
button[0][1].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][1].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][2] + board[1][2] + board[2][2] == -3){
button[0][2].setBackground(Color.red);
button[1][2].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

//Message announcing winner in verticals
if (board[0][0] + board[1][1] + board[2][2] == -3){
button[0][0].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][2] + board[1][1] + board[2][0] == -3){
button[0][2].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][0].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}
}

// In the event of a tie
if (turn == 9){
JOptionPane.showMessageDialog(null,"Tied!");
for (int s = 0; s < 3; ++s) {
for (int r = 0; r < 3; ++r) {
button[s][r].setIcon(blank);
button[s][r].setBackground(Color.white);
turn=0;
board[s][r]=0;}}}
}}});

// Create frame
myframe.setVisible(true);
myframe.setSize(510, 510);
myframe.setIconImage(bi);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}}}