-
TicTacToe help...
I'm stuck on my tictactoe game, i'm just trying to get my users move to show up when they select it, but i cant get it to show up? Can some one see what I did wrong...
(my main class)
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
public class TicTacToe {
//instance variables
private char[] _board;
private BoardDisplay _drawingPanel;
private UserMove _input;
//Size of frame
public static int HEIGHT = 300;
public static int WIDTH = HEIGHT;
public static int BOX = 9;
//TicTacToe constructer
public TicTacToe() {
//Window
JFrame main = new JFrame("TicTacToe!");
main.setSize(WIDTH, HEIGHT);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Drawing Panel
_drawingPanel = new BoardDisplay(this);
main.add(_drawingPanel);
main.setVisible(true);
//Board
_board = new char[BOX];
//User move
_input = new UserMove();
}
public void playGame(){
int box = _input.getUserMove();
_board[box] = 'x';
}
public char[] getBoard(){
return _board;
}
public static void main(String[] args){
TicTacToe ttt = new TicTacToe();
ttt.playGame();
ttt.getBoard();
}
}
(my user move)
import javax.swing.JOptionPane;
public class UserMove {
/**
* Creates a new <code>UserMove</code> instance.
*
*/
public int getUserMove(){
int answer;
do{
String box = JOptionPane.showInputDialog("Pick a number box to place your move");
if(box.equals("1")){
answer = 0;
}
else if(box.equals("2")){
answer = 1;
}
else if(box.equals("3")){
answer = 2;
}
else if(box.equals("4")){
answer = 3;
}
else if(box.equals("5")){
answer = 4;
}
else if(box.equals("6")){
answer = 5;
}
else if(box.equals("7")){
answer = 6;
}
else if(box.equals("8")){
answer = 7;
}
else if(box.equals("9")){
answer = 8;
}
else{
answer = 10;
System.out.print("Not a valid box!");}
}while(answer == 10);
return answer;
}
}
(the board)
/**
* Describe class BoardDisplay here.
*
*
* Created: Mon Dec 4 21:07:50 2006
*
* @author <a href="mailto:f7101315@eagle">John.Santore</a>
* @version 1.0
*/
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
public class BoardDisplay extends JPanel {
private TicTacToe _game;
public static final int VERT1_X = 100;
public static final int VERT_Y1 = VERT1_X/2;
public static final int VERT2_X = VERT1_X+50;
public static final int VERT_Y2 = VERT_Y1+150;
public static final int HORIZ1_Y = VERT1_X;
public static final int HORIZ_X1 = VERT_Y1;
public static final int HORIZ2_Y = HORIZ1_Y+50;
public static final int HORIZ_X2 = HORIZ_X1+150;
public static final String ONE = "1";
public static final String TWO = "2";
public static final String THREE = "3";
public static final String FOUR = "4";
public static final String FIVE = "5";
public static final String SIX = "6";
public static final String SEVEN = "7";
public static final String EIGHT = "8";
public static final String NINE = "9";
public static final String X = "X";
public static final String O = "O";
public static final int COL1_X = 55;
public static final int COL2_X = 105;
public static final int COL3_X = 155;
public static final int ROW1_Y = 65;
public static final int ROW2_Y = 115;
public static final int ROW3_Y = 165;
public static final int BCOL1_X = 75;
public static final int BCOL2_X = 125;
public static final int BCOL3_X = 175;
public static final int BROW1_Y = 85;
public static final int BROW2_Y = 135;
public static final int BROW3_Y = 185;
public BoardDisplay(TicTacToe game) {
_game = game;
}
public void paintComponent(Graphics g){
//Board display
Color original = g.getColor();
g.setColor(Color.BLACK);
g.drawLine(VERT1_X, VERT_Y1, VERT1_X, VERT_Y2);
g.drawLine(VERT2_X, VERT_Y1, VERT2_X, VERT_Y2);
g.drawLine(HORIZ_X1,HORIZ1_Y, HORIZ_X2, HORIZ1_Y);
g.drawLine(HORIZ_X1,HORIZ2_Y, HORIZ_X2, HORIZ2_Y);
g.drawString(ONE, COL1_X, ROW1_Y);
g.drawString(TWO, COL2_X, ROW1_Y);
g.drawString(THREE, COL3_X, ROW1_Y);
g.drawString(FOUR, COL1_X, ROW2_Y);
g.drawString(FIVE, COL2_X, ROW2_Y);
g.drawString(SIX, COL3_X, ROW2_Y);
g.drawString(SEVEN, COL1_X, ROW3_Y);
g.drawString(EIGHT, COL2_X, ROW3_Y);
g.drawString(NINE, COL3_X, ROW3_Y);
g.setColor(original);
// X's and O's
char[] board = _game.getBoard();
for(int i = 0; i <9; i++)
if(board[i] == 'x')
g.drawString(X, (i+20), (i-20)); (<- those coordanites are temp. i'd
figure out the real ones when i get
the code to work...)
else if(board[i] == 'o')
g.drawString(O, (i+20), (i-20));
else if(board[i] == ' ')
g.drawString(" ", 10, 10);
}
}
http://webhost.bridgew.edu/jsantore/...mp101Lab5.html
(that is the website for the whole project ... )
thanks a bunch if you can help :-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks