Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java with OOP assignment (Eclipse)
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Siu
Java with OOP assignment (Eclipse)
1 Answer(s)      4 years and 7 months ago
Posted in : Java Beginners


"THREE Ts GAME"

*Description*

A "tic-tac-toe" game is a two player's board game where the game board is made of 3 X 3 grids. The two players, one being assigned with 'O' and the other with 'X', where each if the two players take turns marking the spaces in a 3 X 3 grid, for example X for the first player. The player who succeeds in placing three respective marks in horizontal, vertical or diagonal row wins the game.

A "toe-tac-tic" game is the opposite of a "tic-tac-toe". In "toe-tac-tic" game, you try to get your opponent to get three in a row either horizontally, vertically or diagonally. The winner is the player who does not get three in a row.

In this assignment, you are required to write both the "tic-tac-toe" and "toe-tac-tic" games using Java. When your game application started, the players will be able to choose either to play "tic-tac-toe" or "toe-tac-tic" game.

*Requirements*

1. The game must be GUI based.
2. Create all the necessary classes. At least one of the classes must be an abstract class with at least one abstract, method and all classes will have one or more constructors.
3. All classes must be part of the threeTsGame package.
4. All errors must be handled.
5. Create the html javadoc for the threeTsGame package.

Advertisement
View Answers

March 23, 2010 at 10:11 AM


Here is what I have done, but in Applet form. Anyone who knows how to convert my algorithm to GUI based program?

Here goes,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class TicTacToe extends JApplet {
// Indicate which player has a turn; initially it is the X player
private char whoseTurn = 'X';

// Create and initialize cells
private Cell[][] cells = new Cell[3][3];

// Create and initialize a status label
private JLabel jlblStatus = new JLabel ("X's turn to play");

/** Initialize UI */
public TicTacToe() {
JPanel p = new JPanel(new GridLayout(3, 3, 0, 0));
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
p.add(cells[i][j] = new Cell());

// Set line borders on the cell panel and the status label
p.setBorder(new LineBorder(Color.red, 1));
jlblStatus.setBorder(new LineBorder(Color.yellow, 1));

// Place the panel and the label to the applet
add(p, BorderLayout.CENTER);
add(jlblStatus, BorderLayout.SOUTH);
}

/** Determine if the cells are all occupied */
public boolean isFull() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (cells[i][j].getToken() == ' ')
return false;

return true;
}

/** Determine if the player with the specified token wins */
public boolean isWon(char token) {
for (int i = 0; i < 3; i++)
if ((cells[i][0].getToken() == token)
&&(cells[i][1].getToken() == token)
&&(cells[i][2].getToken() == token)) {
return true;
}

for (int j = 0; j < 3; j++)
if ((cells[0][j].getToken() == token)
&& (cells[1][j].getToken() == token)
&& (cells[2][j].getToken() == token)) {
return true;
}

if ((cells[0][0].getToken() == token)
&& (cells[1][1].getToken() == token)
&& (cells[2][2].getToken() == token)) {
return true;
}

if ((cells[0][2].getToken() == token)
&& (cells[1][1].getToken() == token)
&& (cells[2][0].getToken() == token)) {
return true;
}

return false;
}

// An inner class for a cell
public class Cell extends JPanel {
// Token used for this cell
private char token = ' ';

public Cell() {
setBorder(new LineBorder(Color.black, 1));
addMouseListener(new MyMouseListener());
}

/**Return token */
public char getToken() {
return token;
}
/** Set a new token */
public void setToken(char c) {
token = c;
repaint();
}

/** Paint the cell */
protected void paintComponent(Graphics g) {
super.paintComponent(g);

if (token == 'X') {
g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
}
else if (token == '0') {
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}
}

private class MyMouseListener extends MouseAdapter {
/** Handle mouse click on a cell */
public void mouseClicked(MouseEvent e) {
// If cell is empty and game is not over
if (token == ' ' && whoseTurn != ' ') {
setToken(whoseTurn);

//Check game status
if (isWon(whoseTurn)) {
jlblStatus.setText(whoseTurn + "Win! The game is over");
whoseTurn = ' ';
}
else if (isFull()) {
jlblStatus.setText("Draw! The game is over");
whoseTurn = ' ';
}
else {
// Change the turn
whoseTurn = (whoseTurn == 'X') ? '0': 'X';
// Display whose turn
jlblStatus.setText(whoseTurn + "'s turn");
}
}
}
}
}
}


Related Tutorials/Questions & Answers:
Java with OOP assignment (Eclipse)
Java with OOP assignment (Eclipse)  How do i control the length of characters in an input? E.g.: Enter your name > Hi * Name too short
Java with OOP assignment (Eclipse) - Java Beginners
Java with OOP assignment (Eclipse)  "THREE Ts GAME" *Description.... In this assignment, you are required to write both the "tic-tac-toe" and "toe-tac-tic" games using Java. When your game application started, the players will be able
Advertisements
JAVA assignment
JAVA assignment  Is there any1 on this site who can write a java... want small java application? Please specify the type of java program you want. Have a look at the following link. This link consists of several java program
java assignment
java assignment  Write an application that allows the user to choose insurance options in JCheckBoxes. Use a ButtonGroup to allow the user to select only one of two insurance types√Ę‚?¨‚??HMO (health maintenance organization
java oop
java oop  Design and implement a class called DayType that implements the day of the week in a program. The class DayType should store the day, such as Sun for Sunday, Mon for Monday and so on and so forth. The program should
Assignment
Assignment  any one can help with a java assignment
java assignment - Java Beginners
java assignment  I got assignment in "design a vehicle class hierarchy in java,write a test program to demonstrate polymorphism".please send me the necessary codings and algorithm.  Hi Friend, Try the following code
OOP - Java Beginners
OOP Design Pattern  What is the oops design patterns
OOP JAVa - Java Interview Questions
OOP JAVa  Is java is 100% OOP.?Plz giv full explanation.  hi Sanjay,dis is Srinivas(MCA from Andhra University).....Java is not an 100 % OOPL,because of the availability of Primitive Data Types
oop answers - Java Beginners
oop answers  Consider the following declarations: { private int.... Write a Java statement that prints the values of the instance variables of x. i. Write a Java statement that creates the XClass object t and initializes
OOP questions - Java Beginners
OOP questions  Consider the following declarations: { private... of a and the instance variable w is initialized to the value of b. h. Write a Java statement that prints the values of the instance variables of x. i. Write a Java
Java assignment of variables - JSP-Servlet
Java assignment of variables   .... var strName="Tarunkanti Kar"; .... ... I want to access the script variable in java variable please give the code
OOP - Java Beginners
OOP  i have problem with OOP hope u'll help me write a program that allows the user to enter student' names followed by their test scores and outputs...://www.roseindia.net/java/master-java/java-object-oriented-language.shtml
OOP questions - Java Beginners
OOP questions  Consider the following declarations: { private int u; private double w; public XClass() { } public XClass(int a, double...; } For more information on Java visit to : http://www.roseindia.net/java
OOP with Java - Java Beginners
OOP with Java  I had a look at the solution for the answers, in the website already, but my lecturer wants us to do it in another method. Here goes...) { Question2 q = new Question2(); q.buildSeats(); q.printSeats
java"oop" - Java Beginners
Java OOPs Concept  What is OOPs programming and what it has to do with the Java?  Hi i hope you understand it.//To print the even numbers...){ System.out.println(e); } }}Java program to display all even numbers http
java oop - Java Beginners
java oop  Consider the following declarations: { private int u... variable w is initialized to the value of b. h. Write a Java statement that prints the values of the instance variables of x. i. Write a Java statement
JAVA OOP - Java Beginners
JAVA OOP  Employees in a company are divided into the classes Employee, HourlyPaid, Salescommissioned and Executive for the purpose of calculating their weekly wages or monthly salaries. The data to be maintained for each class
Assignment operator in java
Assignment operator in java Assignment operator is very common to almost all... the value to the variable lying on the left side. Java assignment operator... to be assigned to the variable to the left side. Assignment operator is also used
OOP - Java Beginners
OOP  Which one is better to learn object oriented progamming java or c++? thaks, Bet.  Hello, As of my concern both have different scope and use, you can not merge these. As i java person so i will show you java
OOP - Java Beginners
); } For more information on Java visit to : http://www.roseindia.net/java/ http://www.roseindia.net/java/beginners/Construct.shtml Thanks
OOP using Java - Java Beginners
OOP using Java  Can you write a Java statement that creates the object mysteryClock of the Clock type, and initialize the instance variables hr,min...); } } For more information on Java visit to : http://www.roseindia.net/java
OOP with Java 2 - Java Beginners
OOP with Java 2  Define a class called BogEntry that could be used to store an entry for a Web log. The class should have member variables to store the poster's username, text of entry, and the date of the entry using the Date
assignment
assignment  Write a Java program that reads a file and prints the amount of palindromes contained within the file
assignment
assignment  Hi guys i wanna Know how to code for the following output in java. * ** *** **** ***** please guys if there is any1 who can help please do.  System.out.printf
assignment
: Write a Java program that will declare an array of THREE (3) Circle objects based
OOP Using JAVA - Java Beginners
OOP Using JAVA  OBJECT ORIENTED PROGRAMMING USING JAVA (hope guys u will help me please i need your help,thank you so much) Create a Java program...(); oops.OopType(); } } For more information on Java visit to : http
OOP with Java 3 - Java Beginners
OOP with Java 3  Write a Temperature class that has two instances variables: temperature value (a floating-point number) and a character for the scale, wither C for Celsius or F for Fahrenheit. the class should have four
OOP with Java-Array - Java Beginners
OOP with Java-Array  Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows: 1 A B C D 2 A B C D 3 A B C D 4 A B C D 5 A B C D 6 A B C D 7
oop
oop  can you tell me about why java is not 100% object oriented language
Masters of Java Assignment Plugin
Masters of Java Assignment Plugin  ... of the Eclipse Java IDE.  The plugin has the following features : New Masters of Java Assignment Wizard which sets up project structure
Advertisements
 

 

 

DMCA.com