A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Applets
Author
problem drawing TicTacToe
Jorge Vazquez
Greenhorn
Joined: Nov 04, 2012
Posts: 7
posted
Jan 06, 2013 19:04:51
0
guys, I'm trying to implement a TicTacToe game, but so far I've been unable to draw the gid.... any help appreciated
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.LineBorder; /** * Game TicTacToe Applet * @author Jorge L. Vazquez * 1/6/13 */ public class TicTacToe extends JApplet { private char whosTurn = 'X'; //whos turn char private JLabel jlbStatus = new JLabel("X turn"); //displays game status private Cell[][] cells = new Cell[3][3]; //all cells public void TicTacToe() { JPanel panel = new JPanel(new GridLayout(3,3,0,0)); for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { cells[i][j] = new Cell(); panel.add(cells[i][j]); } } //set line border on grid and status label panel.setBorder(new LineBorder(Color.RED)); jlbStatus.setBorder(new LineBorder(Color.YELLOW)); //placing the panel and status label in applet add(panel, BorderLayout.CENTER); add(jlbStatus, BorderLayout.SOUTH); } //checks for end of game(full cells) 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; } //checks for winners public boolean isWon(char token) { //checking horizontals for(int i = 0; i < 3; i++) { if(cells[i][0].getToken() == cells[i][1].getToken() && cells[i][1].getToken() == cells[i][2].getToken()) return true; } //checking verticals for (int i = 0; i < 3; i++) { if(cells[0][i].getToken() == cells[1][i].getToken() && cells[1][i].getToken() == cells[2][i].getToken()) return true; } //checking diagonals if(cells[0][0].getToken() == cells[1][1].getToken() && cells[1][1].getToken() == cells[2][2].getToken()) return true; if(cells[0][2].getToken() == cells[1][1].getToken() && cells[1][1].getToken() == cells[2][0].getToken()) return true; return false; } //cell inner class class Cell extends JPanel { //holds X or O private char token = ' '; public Cell() { setBorder(new LineBorder(Color.BLACK, 1)); //cell border //addMouseListener(new MyMouseListener()); //register mouse listener } /** getters and setters */ public char getToken() { return token; } public void setToken(char token) { this.token = token; repaint(); } /** paint the component on a cell */ public void paintComponet(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 { /** handling mouse clicks in a cell */ @Override public void mouseClicked(MouseEvent e) { //if cell is empty and game not over if (token == ' ' && whosTurn != ' ') { setToken(whosTurn); //checking for winners if (isWon(token)) { //output game winner jlbStatus.setText(whosTurn + " won game!"); //set whosTurn blank whosTurn = ' '; } //check for draw else if (isFull()) { //output game status jlbStatus.setText("game is a draw!"); //set whosTurn to blank whosTurn = ' '; } //change turn and update status else { whosTurn = (whosTurn == 'X') ? '0' : 'X'; jlbStatus.setText(whosTurn + " turn"); } } } } } }
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
Jan 06, 2013 19:12:46
0
possibly, this line
public void TicTacToe() {
should be
public void init() {
Jorge Vazquez
Greenhorn
Joined: Nov 04, 2012
Posts: 7
posted
Jan 06, 2013 23:36:43
0
Michael Dunn wrote:
possibly, this line
public void TicTacToe() {
should be
public void init() {
Thanks for the help... it works now
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: problem drawing TicTacToe
Similar Threads
Game Tutorials -->> TicTacToe
TicTacToe Game
TicTacToe Game..
TicTacToe game
modifying a tic tac toe game
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter