Hi, I am doing a assigment for a programming class, which involved setting up a client-server archtecture for a tic-tac-toe game. The problem I am having is implementing the GUI on top of this. It is based on a mvc design
pattern.
The problem I am having is accessing the button's from the controller class which has a instace of the view. I need to be able to hit a button on a gui, and then send the information from the controller to the game model. But I can't access the buttons from the controller.
included GameController and GameGUI classes.
Thanks, Funkster9000
package gameUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameGUI extends JFrame implements ActionListener
{
private JLabel title;
private JLabel userMessage;
private JLabel userPrompt;
private GameControllerSC theController;
public JButton TopLeft;
public JButton TopMiddle;
public JButton TopRight;
public JButton MiddleLeft;
public JButton MiddleMiddle;
public JButton MiddleRight;
public JButton BottomLeft;
public JButton BottomMiddle;
public JButton BottomRight;
public JButton help;
public GameGUI()
{
buildGUI();
setTitle("TICTACTOE");
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void buildGUI()
{
GridLayout layout = new GridLayout(7, 1);
setLayout(layout);
JPanel row1 = new JPanel();
title = new JLabel("Let's Play Tic Tac Toe!");
JPanel row2 = new JPanel();
userPrompt = new JLabel("Connected to server");
JPanel row3 = new JPanel();
TopLeft = new JButton("?");
TopLeft.addActionListener(this);
TopMiddle = new JButton("?");
TopMiddle.addActionListener(this);
TopRight = new JButton("?");
TopRight.addActionListener(this);
JPanel row4 = new JPanel();
MiddleLeft = new JButton("?");
MiddleLeft.addActionListener(this);
MiddleMiddle = new JButton("?");
MiddleMiddle.addActionListener(this);
MiddleRight = new JButton("?");
MiddleRight.addActionListener(this);
JPanel row5 = new JPanel();
BottomLeft = new JButton("?");
BottomLeft.addActionListener(this);
BottomMiddle = new JButton("?");
BottomMiddle.addActionListener(this);
BottomRight = new JButton("?");
BottomRight.addActionListener(this);
JPanel row6 = new JPanel();
userMessage = new JLabel();
JPanel row7 = new JPanel();
FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER, 5, 5);
row1.setLayout(layout2);
row1.add(title);
add(row1);
row2.setLayout(layout2);
row2.add(userPrompt);
add(row2);
row3.setLayout(layout2);
row3.add(TopLeft);
row3.add(TopMiddle);
row3.add(TopRight);
add(row3);
row4.setLayout(layout2);
row4.add(MiddleLeft);
row4.add(MiddleMiddle);
row4.add(MiddleRight);
add(row4);
row5.setLayout(layout2);
row5.add(BottomLeft);
row5.add(BottomMiddle);
row5.add(BottomRight);
add(row5);
row6.setLayout(layout2);
row6.add(userMessage);
add(row6);
row7.setLayout(layout2);
add(row7);
}
public void getController(GameControllerSC controller){
theController = controller;
}
public void displayMessage(
String message){
String tempMessage = message;
userMessage.setText(message);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == MiddleLeft){
}
}
/*
* GameControllerSC.java
*
* Created on July 23, 2008, 2:06 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package gameUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.JButton;
import gameModel.GameModel;
/**
*
*
*/
public class GameControllerSC {
private GameModel model;
private GameView view;
private boolean gameOver = false;
private int port = 4444;
private int col = 0;
private int row = 0;
private String [][]theBoard;
private String player = "X";
String hostname = "localhost";
Socket clientSocket1 = null;
Socket clientSocket2 = null;
Socket toServer = null;
BufferedReader in = null;
BufferedReader in1 = null;
BufferedReader in2 = null;
PrintWriter out1 = null;
PrintWriter out2 = null;
BufferedReader consoleIn = null;
PrintWriter out = null;
String message;
/** Creates a new instance of GameControllerSC */
public GameControllerSC(GameModel model) throws Exception {
this.model = model;
serverStart();
}
public GameControllerSC(GameView view){
this.view = view;
buildBoard();
clientStart();
}
private void displayBoard(){
for(int i = 0; i < 4; i++)
{
int j = 0;
view.displayMessage(theBoard[i][j] + theBoard[i][j+1] + theBoard[i][j+2] + theBoard[i][j+3]);
}
}
private void buildBoard(){
theBoard = new String[4][4];
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(i == 0 && j == 0)
theBoard[i][j] = " ";
if(i == 0 && j > 0)
theBoard[i][j] = "" + j;
if(i > 0 && j == 0)
theBoard[i][j] = "" + i;
if(i > 0 && j > 0)
theBoard[i][j] = "*";
}
}
}
private void serverStart() throws Exception{
ServerSocket serverSocket1 = new ServerSocket(port);
System.err.println("Started server on port " + port);
while(true){
Socket clientSocket1 = serverSocket1.accept();
System.out.println("Accepted connection from client1");
Socket clientSocket2 = serverSocket1.accept();
System.out.println("Accepted connection from client2");
BufferedReader in1 = new BufferedReader(new InputStreamReader(clientSocket1.getInputStream()));
PrintWriter out1 = new PrintWriter(clientSocket1.getOutputStream());
BufferedReader in2 = new BufferedReader(new InputStreamReader(clientSocket2.getInputStream()));
PrintWriter out2 = new PrintWriter(clientSocket2.getOutputStream());
sendMessage(out1, "X");
sendMessage(out2, "O");
int i = 2;
String gameMessage = null;
while(gameOver != true){
if(i%2 == 0){//player x's turn'
player = "X";
gameMessage = "" + row + col;
sendMessage(out1, gameMessage);
col = Integer.parseInt(in1.readLine());
row = Integer.parseInt(in1.readLine());
if(model.checkMove(row, col, player) == true){
gameMessage = model.checkGame();
if(gameMessage != "continue"){
gameOver = true;
if(gameMessage != player){
sendMessage(out1, "tie");
}
else{
sendMessage(out1, "win");
}
}
i = 1;
sendMessage(out1, "continue");
}
else
sendMessage(out1,"illegal");
}
else{
player = "O";
gameMessage = "" + row + col;
sendMessage(out2, gameMessage);
col = Integer.parseInt(in2.readLine());
row = Integer.parseInt(in2.readLine());
if(model.checkMove(row, col, player) == true){
gameMessage = model.checkGame();
if(gameMessage != "continue"){
gameOver = true;
if(gameMessage != player){
sendMessage(out2, "tie");//games ends in tie
}
else{
sendMessage(out2, "win");//game ends in win
}
}
i = 2;
sendMessage(out2, "continue");
}
else
sendMessage(out2,"illegal");
}
}
in1.close();
in2.close();
out1.close();
out2.close();
clientSocket1.close();
clientSocket2.close();
serverSocket1.close();
}
}
private void clientStart(){
try{
consoleIn = new BufferedReader(new InputStreamReader(System.in));
toServer = new Socket(hostname, 4444);
in = new BufferedReader(new InputStreamReader(toServer.getInputStream()));
out = new PrintWriter(toServer.getOutputStream());
view.displayMessage("Connected to server!");
view.displayMessage("Game is about to begin");
player = in.readLine();
view.displayMessage("you are player " + player);
while(gameOver!= true){
String gameMessage;
String row;
String col;
int parseRow;
int parseCol;
gameMessage = in.readLine();
view.displayMessage(gameMessage);
row = gameMessage.substring(0,1);
col = gameMessage.substring(1,2);
System.out.println(col);
System.out.println(row);
if(col.equals("0") && row.equals("0"))
displayBoard();
else{
parseRow = Integer.parseInt(row);
parseCol = Integer.parseInt(col);
if(player.equals("X"))
theBoard[parseRow][parseCol] = "O";
else
theBoard[parseRow][parseCol] = "X";
displayBoard();
}
view.displayMessage("enter column: ");
parseCol = Integer.parseInt(getInput());
gameMessage = "" + parseCol;
sendMessage(out, gameMessage);//send column to server
view.displayMessage("enter row: ");
parseRow = Integer.parseInt(getInput());
gameMessage = "" + parseRow;
sendMessage(out, gameMessage);//send row to server
gameMessage = in.readLine();
if(gameMessage.equals("illegal")){
view.displayMessage("Re-enter a valid position which is open");
}
if(gameMessage.equals("continue")){
view.displayMessage("valid move, please wait for your opponent to go");
theBoard[parseRow][parseCol] = player;
}
if(gameMessage.equals("tie")){
view.displayMessage("game is tie");
theBoard[parseRow][parseCol] = player;
gameOver = true;
}
if(gameMessage.equals("win")){
view.displayMessage("you win");
theBoard[parseRow][parseCol] = player;
gameOver = true;
}
}
}catch(IOException e){
System.err.println("error" + e.getMessage());
}finally{
try{
System.err.println("Closing connection with server");
in.close();
out.close();
toServer.close();
consoleIn.close();
}
catch(IOException e){
System.err.println("error: " + e.getMessage());
}
}
}
/**
* Method that is given a message and then sends that message to the chosen stream.
* @param PrintWriter x
* @param String s
*/
private void sendMessage(PrintWriter x, String s){
x.println(s);
x.flush();
}
/**
* Method that recieves input from the client, validates this input, and then returns it.
* @return String input
*/
private String getInput(){
String theLine = null;
int input = 0;
while(true){
try{
theLine = consoleIn.readLine();
input = Integer.parseInt(theLine);
if(input < 4 && input > 0){
return theLine;
}
else
view.displayMessage("Re-enter a valid number");
}catch (IOException ex) {
System.err.println("error: " + ex.getMessage());
}
}
}
}