TicTacToe i cockety – jak to uruchomić?

0

Cześć,
Mój problem polega na tym, że nie wiem już kompletnie co mam zrobić niby wszystko jest, ale nie mam pojecia jak to uruchomić. Sockety to dla mnie ciemna strefa totalnie i na prawdę błagałbym kogoś aby mógł zrobić tak żeby to działało :(.

import java.awt.GridLayout; 
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;


public class Main extends JFrame
{
	
	//false = krzyzyk, true kolko
	public static boolean type = false;
	//tablica z przyciskami
	public static JButton[][] buttons = new JButton[3][3];
	public static int counter = 0;
	
	public Main()
	{
		/// Określa rozmiar, widoczność, nazwa wyświetlanego programu itp
		setSize(450,450);
		setVisible(true);
		setTitle("TicTacToe");
		/// Określa tablice? 3x3
		setLayout(new GridLayout(3,3));
		/// funkcja okresla zeby do kazdej komorki byl przypisany przycisk
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j <3; j++)
			{
				myButton button = new myButton("", i, j);
				buttons[i][j] = button;
				button.addActionListener(new buttonHandler());
				add(button);
			}
		}
	}
	public static void main(String[] args)  {
		EventQueue.invokeLater(new Runnable(){
			@Override
			public void run(){
				new Main();
			}
		});	
	}
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;


public class buttonHandler implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e)
	{
		myButton button = (myButton) e.getSource();
		if(Main.type)
		{
			button.setText("O");
			Main.type = false;
		}
		else
		{
			button.setText("X");
			Main.type = true;
		}
		button.setEnabled(false);
		Main.counter++;
		checkIfEnded(button);
		
	}

	
	private void checkIfEnded(myButton button)
	{
		
		int indexI = button.indexI;
		int indexJ = button.indexJ;
		String sign = button.getText();
		
		if(Main.counter >= 9)
        {
            JOptionPane.showMessageDialog(null, "Remis", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
        }
		else
		if(indexI == 0)
		{	
			if(indexJ == 0)
			{
				if((Main.buttons[0][1].getText() == sign && Main.buttons[0][2].getText() == sign) ||
					(Main.buttons[1][0].getText() == sign && Main.buttons[2][0].getText() == sign) ||
					(Main.buttons[1][1].getText() == sign && Main.buttons[2][2].getText() == sign))
					{
						JOptionPane.showMessageDialog(null,"Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}
			}
			else if(indexJ == 1)
			{
				if((Main.buttons[0][0].getText() == sign && Main.buttons[0][2].getText() == sign) ||
					(Main.buttons[1][1].getText() == sign && Main.buttons[2][1].getText() == sign))
					{
						JOptionPane.showMessageDialog(null, "Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}
			}
			else
			{
				if((Main.buttons[0][0].getText() == sign && Main.buttons[0][1].getText() == sign) ||
					(Main.buttons[1][2].getText() == sign && Main.buttons[2][2].getText() == sign) ||
					(Main.buttons[1][1].getText() == sign && Main.buttons[2][0].getText() == sign))
					{
						JOptionPane.showMessageDialog(null, "Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}
			}
		}
		else if(indexI == 1)
		{
			if(indexJ == 0)
			{
				if((Main.buttons[0][0].getText() == sign && Main.buttons[2][0].getText() == sign) ||
					(Main.buttons[1][1].getText() == sign && Main.buttons[1][2].getText() == sign))
					{
						JOptionPane.showMessageDialog(null, "Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}				
				
			}
			else if(indexJ == 1)
			{
				if((Main.buttons[1][0].getText() == sign && Main.buttons[1][2].getText() == sign) ||
					(Main.buttons[0][1].getText() == sign && Main.buttons[2][1].getText() == sign) ||
					(Main.buttons[0][0].getText() == sign && Main.buttons[2][2].getText() == sign) ||
					(Main.buttons[0][2].getText() == sign && Main.buttons[2][0].getText() == sign))
					{
						JOptionPane.showMessageDialog(null, "Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}
			}
			else
			{
				if((Main.buttons[0][2].getText() == sign && Main.buttons[2][2].getText() == sign) ||
					(Main.buttons[1][0].getText() == sign && Main.buttons[1][1].getText() == sign))
					{
						JOptionPane.showMessageDialog(null, "Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}
			}
			
		}
		else
		{
			if(indexJ == 0)
			{
				if((Main.buttons[2][1].getText() == sign && Main.buttons[2][2].getText() == sign) ||
					(Main.buttons[1][0].getText() == sign && Main.buttons[0][0].getText() == sign)||
					(Main.buttons[1][1].getText() == sign && Main.buttons[0][2].getText() == sign))
					{
						JOptionPane.showMessageDialog(null, "Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}				
				
			}
			else if(indexJ == 1)
			{
				if((Main.buttons[2][0].getText() == sign && Main.buttons[2][2].getText() == sign) ||
					(Main.buttons[0][1].getText() == sign && Main.buttons[1][1].getText() == sign))
					{
						JOptionPane.showMessageDialog(null, "Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}	
			}
			else
			{
				if((Main.buttons[2][0].getText() == sign && Main.buttons[2][1].getText() == sign) ||
					(Main.buttons[0][2].getText() == sign && Main.buttons[1][2].getText() == sign)||
					(Main.buttons[1][1].getText() == sign && Main.buttons[0][0].getText() == sign))
					{
						JOptionPane.showMessageDialog(null, "Wygrywa - " + sign, "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
					}	
			}
		}
		
	}
}

import javax.swing.JButton;

public class myButton extends JButton{
	
	public int indexI;
	public int indexJ;
	
	
	public myButton(String _text, int _indexI, int _indexJ)
	{
		super();
		this.setText(_text);
		this.indexI = _indexI;
		this.indexJ = _indexJ;
	}

}



import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * A TCP server that runs on port 9090.  When a client connects, it
 * sends the client the current date and time, then closes the
 * connection with that client.  Arguably just about the simplest
 * server you can write.
 */
public class Server {

    /**
     * Runs the server.
     */
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(9090);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out =
                        new PrintWriter(socket.getOutputStream(), true);
                    out.println(new Date().toString());
                } finally {
                    socket.close();
                }
            }
        }
        finally {
            listener.close();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import javax.swing.JOptionPane;

/**
 * Trivial client for the date server.
 */
public class ServerClient {

    /**
     * Runs the client as an application.  First it displays a dialog
     * box asking for the IP address or hostname of a host running
     * the date server, then connects to it and displays the date that
     * it serves.
     */
    public static void main(String[] args) throws IOException {
        String serverAddress = JOptionPane.showInputDialog(
            "Enter IP Address of a machine that is\n" +
            "running the date service on port 9090:");
        Socket s = new Socket(serverAddress, 9090);

        BufferedReader input =
            new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        JOptionPane.showMessageDialog(null, answer);
        System.exit(0);
    }
}


0

xxxxxxxxxxxxxxxxxxxxxxxxxx - tu była linijka ze złośliwościami, ale ją wyciąłem.

Patrz! Ktoś już zrobił odpowiedź na twoje błaganie:
https://github.com/kfr2/java-tictactoe

1 użytkowników online, w tym zalogowanych: 0, gości: 1