Important alert: (current site time 7/15/2013 5:12:17 AM EDT)
 

VB icon

TicTac Game

Email
Submitted on: 5/2/2013 10:07:25 AM
By: Creator_1493  
Level: Beginner
User Rating: Unrated
Compatibility: Java (JDK 1.4), Java (JDK 1.5)
Views: 596
(About the author)
 
     play and enjoy it

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: TicTac Game
// Description:play and enjoy it
// By: Creator_1493
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7138&lngWId=2//for details.//**************************************

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.*;
import java.awt.event.*;
/**********************************************/
/*USING 'X' FOR HUMAN AND '0' FOR COMPUTER*/
/**********************************************/
public class TicTacMine extends java.applet.Applet implements 
ActionListener{
static Button squares[],nGameB;
static Label label,aboutL;
static int squaresRemaining=9;
static String forAbout="",difLevel="";
public void init(){
setSize(500,500);
setLayout(new BorderLayout());
setFont(new Font("TimesRoman",Font.BOLD,30));
Panel p1=new Panel();
p1.setLayout(new GridLayout(3,3));
add(p1,BorderLayout.CENTER);
label=new Label("TIC TAC",1);
label.setBackground(Color.black);
label.setForeground(Color.white);
add(label,BorderLayout.SOUTH);
Panel p2=new Panel();
p2.setLayout(new FlowLayout());
p2.setBackground(Color.black);
//		NEW GAME BUTTON
nGameB=new Button("New Game");
nGameB.setBackground(Color.GREEN);
nGameB.setForeground(Color.yellow);
nGameB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
	for(int i=0;i<9;i++){
	squares[i].setEnabled(true);
	squares[i].setLabel("");
	squares[i].setBackground(new Color(98,156,245));
	}
squaresRemaining=9;
label.setText("TIC TAC");
label.setAlignment(1);
nGameB.setEnabled(false);
}
});
p2.add(nGameB);
//		ABOUT
aboutL=new Label("About");
aboutL.setBackground(Color.black);
aboutL.setForeground(Color.white);
aboutL.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
forAbout=label.getText();
aboutL.setForeground(Color.red);
label.setText("Creator1493");
label.setAlignment(1);
}
});
aboutL.addMouseListener(new MouseAdapter(){
public void mouseExited(MouseEvent e){
aboutL.setForeground(Color.white);
label.setText(forAbout);
label.setAlignment(1);
}
});
p2.add(aboutL);
add(p2,BorderLayout.NORTH);
squares=new Button[9];
for(int i=0;i<9;i++){
		squares[i]=new Button();
		squares[i].addActionListener(this);
		squares[i].setBackground(new Color(98,156,245));
		p1.add(squares[i]);
}
nGameB.setEnabled(false);
}//end init
public void actionPerformed(ActionEvent e) {
for ( int i=0; i<9; i++ ) {
 if ( e.getSource() == squares[i] ) {
if ( squares[i].getLabel().equals("X") ||
squares[i].getLabel().equals("O") ) {
return;
}
squares[i].setLabel("X");
squaresRemaining--;
String winner = "";
winner = checkForWin();
if ( winner.equals("X") || winner.equals("O") ) {
endGame();
} else {
if ( squaresRemaining <= 0 ) {
endGame();
} else {
//		for animation
			try{
			Thread.sleep(100);}
			catch(InterruptedException w){}
			computerMove();
squaresRemaining--;
winner = checkForWin();
if ( winner.equals("X") || winner.equals("O") // if 
|| squaresRemaining <= 0 )// or if tie
{
endGame();
}
}
}
}
}
} // end actionPerformed
String checkForWin() {
String theWinner = "";
if ( squares[0].getLabel().equals(squares[1].getLabel()) &&
 squares[0].getLabel().equals(squares[2].getLabel()) &&
 ! squares[0].getLabel().equals("") ) {
 theWinner = squares[0].getLabel();
 colorLine(0,1,2,theWinner);
} else if ( squares[3].getLabel().equals(squares[4].getLabel()) &&
 squares[3].getLabel().equals(squares[5].getLabel()) &&
 ! squares[3].getLabel().equals("") ) {
 theWinner = squares[3].getLabel();
 colorLine(3,4,5,theWinner);
} else if ( squares[6].getLabel().equals(squares[7].getLabel()) &&
 squares[6].getLabel().equals(squares[8].getLabel()) &&
 ! squares[6].getLabel().equals("") ) {
 theWinner = squares[6].getLabel();
 colorLine(6,7,8,theWinner);
} else if ( squares[0].getLabel().equals(squares[3].getLabel()) &&
 squares[0].getLabel().equals(squares[6].getLabel()) &&
 ! squares[0].getLabel().equals("") ) {
 theWinner = squares[0].getLabel();
 colorLine(0,3,6,theWinner);
} else if ( squares[1].getLabel().equals(squares[4].getLabel()) &&
 squares[1].getLabel().equals(squares[7].getLabel()) &&
 ! squares[1].getLabel().equals("") ) {
 theWinner = squares[1].getLabel();
 colorLine(1,4,7,theWinner);
} else if ( squares[2].getLabel().equals(squares[5].getLabel()) &&
 squares[2].getLabel().equals(squares[8].getLabel()) &&
 ! squares[2].getLabel().equals("") ) {
 theWinner = squares[2].getLabel();
 colorLine(2,5,8,theWinner);
} else if ( squares[0].getLabel().equals(squares[4].getLabel()) &&
 squares[0].getLabel().equals(squares[8].getLabel()) &&
 ! squares[0].getLabel().equals("") ) {
 theWinner = squares[0].getLabel();
 colorLine(0,4,8,theWinner);
} else if ( squares[2].getLabel().equals(squares[4].getLabel()) &&
 squares[2].getLabel().equals(squares[6].getLabel()) &&
 ! squares[2].getLabel().equals("") ) {
 theWinner = squares[2].getLabel();
 colorLine(2,4,6,theWinner);
}
return theWinner;
}
void computerMove() {
int pickedSquare;
pickedSquare = tryToWin();
if ( pickedSquare == -1 )
pickedSquare = makeBlock();
 if ( (pickedSquare == -1)&&(squares[4].getLabel().equals("")) )
	pickedSquare=4;
	 if ( pickedSquare == -1 )
pickedSquare = pickRandom();
squares[pickedSquare].setLabel("O");
return;
}
int tryToWin() {
// Since computer plays "O", find empty square that is in line with 
return EmptySquare("O");
}
int makeBlock() {
// Since human plays "X", find empty square that is in line with 
return EmptySquare("X");
}
int EmptySquare(String player) {
int a[] = new int[9];
	for ( int i = 0; i < 9; i++ ) {
if ( squares[i].getLabel().equals("O") )
a[i] = -1;
else if ( squares[i].getLabel().equals("X") )
a[i] = 1;
else
a[i] = 0;
}
int sum = player.equals("O") ? -2 : 2;
// Top Row
if ( a[0] + a[1] + a[2] == sum ) {
if ( a[0] == 0 )
return 0;
else if ( a[1] == 0 )
return 1;
else
return 2;
}
// Middle Row
if ( a[3] +a[4] + a[5] == sum ) {
if ( a[3] == 0 )
return 3;
else if ( a[4] == 0 )
return 4;
else
return 5;
}
// Bottom Row
if ( a[6] + a[7] +a[8] == sum ) {
if ( a[6] == 0 )
return 6;
else if ( a[7] == 0 )
return 7;
else
return 8;
}
// Left column
if ( a[0] + a[3] + a[6] == sum ) {
if ( a[0] == 0 )
return 0;
else if ( a[3] == 0 )
return 3;
else
return 6;
}
// Middle column
if ( a[1] +a[4] + a[7] == sum ) {
if ( a[1] == 0 )
return 1;
else if ( a[4] == 0 )
return 4;
else
return 7;
}
// right column
if ( a[2] + a[5] + a[8] == sum ) {
if ( a[2] == 0 )
return 2;
else if ( a[5] == 0 )
return 5;
else
return 8;
}
// diagonal upper-left to lower-right
if ( a[0] + a[4] + a[8] == sum ) {
if ( a[0] == 0 )
return 0;
else if ( a[4] == 0 )
return 4;
else
return 8;
}
// diagonal upper-right to lower-left
if ( a[2] + a[4] + a[6] == sum ) {
if ( a[2] == 0 )
return 2;
else if ( a[4] == 0 )
return 4;
else
return 6;
}
// not able to find
return -1;
}
int pickRandom() {
boolean found = false;
int pickedSquare = -1;
do {
pickedSquare = (int) (Math.random() * 9 ); // 0 to 8
if ( ! squares[pickedSquare].getLabel().equals("X") &&
 ! squares[pickedSquare].getLabel().equals("O") ) {
found = true;
}
} while ( ! found );
return pickedSquare;
} // end pickRandom()
void colorLine(int a, int b, int c,String win) {
// mark line red if user (X) wins
// mark line blue if computer (O) wins
if ( win.equals("X") ) {
squares[a].setBackground(Color.black);
squares[b].setBackground(Color.black);
squares[c].setBackground(Color.black);
		label.setText("HUMAN WINS");
		label.setForeground(Color.white);
		label.setAlignment(1);
} else {
squares[a].setBackground(Color.black);
squares[b].setBackground(Color.black);
squares[c].setBackground(Color.black);
		label.setText(" COMPUTER WINS");
				label.setForeground(Color.white);
				label.setAlignment(1);
}
}
void endGame(){
for(int i=0;i<9;i++)
squares[i].setEnabled(false);
nGameB.setEnabled(true);
}
}


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/9/2013 6:21:05 PMKwitherspoon

Error: Main method not found in class TicTacMine, please define the main method as:
public static void main(String[] args)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.