Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JLabel;
- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.Label;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class TicTac {
- JPanel windowContent;
- JButton[] squares = new JButton[9];
- JButton newGameButton;
- JLabel wonScore;
- JLabel loseScore;
- JLabel score;
- JPanel panel1;
- JPanel panel2;
- JPanel panel3;
- int emptySquaresLeft=9;
- int won = 0;
- int lose = 0;
- public static void main () {
- JPanel windowContent = new JPanel();
- BorderLayout bl = new BorderLayout();
- windowContent.setLayout(bl);
- JLabel wonScore = new JLabel();
- JLabel loseScore = new JLabel();
- JPanel panel1 = new JPanel();
- windowContent.add("North", panel1);
- JButton newGameButton = new JButton();
- newGameButton.setBackground(Color.GREEN);
- wonScore = new JLabel ("Побед: 0");
- loseScore = new JLabel ("Поражений: 0");
- panel1.setLayout(bl);
- panel1.add(newGameButton);
- panel1.add(wonScore);
- panel1.add(loseScore);
- JLabel score = new JLabel ("Твой ход!");
- windowContent.add(score, "South");
- JPanel panel2 = new JPanel();
- windowContent.add("Center", panel2);
- JButton[] squares = new JButton[9];
- for (int i=0; i<10; i++) {
- squares[i] = new JButton (""+i);
- squares[i].addActionListener(actionPerformed); /*!!!! Cant be resolved to a variable !!!*/
- squares[i].setBackground(Color.ORANGE);
- panel2.add(squares[i]);
- }
- }
- public void actionPerformed (ActionEvent e){
- JButton theButton = (JButton) e.getSource();
- if (theButton == newGameButton){
- for (int i=0; i<9; i++) {
- squares[i].setEnabled(true);
- squares[i].setLabel("");
- squares[i].setBackground(Color.ORANGE);
- }
- emptySquaresLeft=9;
- score.setText("Твой ход!");
- newGameButton.setEnabled(false);
- return;
- }
- String winner="";
- for (int i=0; i<9; i++) {
- if (theButton == squares[i]){
- squares[i].setLabel("X");
- winner = lookForWinner();
- squares[i].setEnabled(false);
- if (!"".equals(winner)) {
- endTheGame ();
- } else {
- computerMove();
- winner = lookForWinner();
- if (!"".equals(winner)) {
- endTheGame();
- }
- }
- break;
- }
- }
- if (winner.equals("X")) {
- score.setText("Ты победил!");
- won++;
- wonScore.setText("Побед: "+won);
- } else if (winner.equals("O")) {
- score.setText("Лох!");
- lose++;
- loseScore.setText("Поражений: "+lose);
- } else if (winner.equals("T")) {
- score.setText("Победила дружба..");
- }
- }
- String lookForWinner() {
- String theWinner="";
- emptySquaresLeft--;
- if (emptySquaresLeft == 0) {
- return "T";
- }
- if (!squares[0].getLabel().equals("")&&
- squares[0].getLabel().equals(squares[1].getLabel()) &&
- squares[0].getLabel().equals(squares[2].getLabel())) {
- theWinner = squares [0].getLabel();
- highlightWinner(0,1,2);
- } else if (!squares[3].getLabel().equals("")&&
- squares[3].getLabel().equals(squares[4].getLabel()) &&
- squares[3].getLabel().equals(squares[5].getLabel())) {
- theWinner = squares [3].getLabel();
- highlightWinner(3,4,5);
- } else if (!squares[6].getLabel().equals("")&&
- squares[6].getLabel().equals(squares[7].getLabel()) &&
- squares[6].getLabel().equals(squares[8].getLabel())) {
- theWinner = squares [6].getLabel();
- highlightWinner(6,7,8);
- } else if (!squares[0].getLabel().equals("")&&
- squares[0].getLabel().equals(squares[3].getLabel()) &&
- squares[0].getLabel().equals(squares[6].getLabel())) {
- theWinner = squares [3].getLabel();
- highlightWinner(0,3,6);
- } else if (!squares[1].getLabel().equals("")&&
- squares[1].getLabel().equals(squares[4].getLabel()) &&
- squares[1].getLabel().equals(squares[7].getLabel())) {
- theWinner = squares [1].getLabel();
- highlightWinner(1,4,7);
- } else if (!squares[2].getLabel().equals("")&&
- squares[2].getLabel().equals(squares[5].getLabel()) &&
- squares[2].getLabel().equals(squares[8].getLabel())) {
- theWinner = squares [2].getLabel();
- highlightWinner(2,5,8);
- } else if (!squares[0].getLabel().equals("")&&
- squares[0].getLabel().equals(squares[4].getLabel()) &&
- squares[0].getLabel().equals(squares[8].getLabel())) {
- theWinner = squares [0].getLabel();
- highlightWinner(0,4,8);
- } else if (!squares[2].getLabel().equals("")&&
- squares[2].getLabel().equals(squares[4].getLabel()) &&
- squares[2].getLabel().equals(squares[6].getLabel())) {
- theWinner = squares [2].getLabel();
- highlightWinner(2,4,6);
- }
- return theWinner;
- }
- void computerMove() {
- int selectedSquare;
- selectedSquare = findEmptySquare("O");
- if (selectedSquare == -1)
- selectedSquare = findEmptySquare("X");
- if ((selectedSquare == -1) && (squares[4].getLabel().equals(""))) {
- selectedSquare=4;
- }
- if (selectedSquare == -1) {
- selectedSquare = getRandomSquare();
- }
- squares[selectedSquare].setLabel("O");
- squares[selectedSquare].setEnabled(false);
- }
- int findEmptySquare(String Player) {
- int weight[] =new int[9];
- for (int i=0; i<9; i++) {
- if (squares[i].getLabel().equals("O"))
- weight[i]=-1;
- else if (squares[i].getLabel().equals("X"))
- weight[i]=1;
- else
- weight[i]=0;
- }
- int twoWeights = Player.equals("O") ? -2 : 2;
- if (weight[0] + weight[1] + weight[2] == twoWeights) {
- if (weight[0] == 0)
- return 0;
- else if (weight[1] == 0)
- return 1;
- else
- return 2;
- }
- if (weight[3] + weight[4] + weight[5] == twoWeights) {
- if (weight[3] == 0)
- return 3;
- else if (weight[4] == 0)
- return 4;
- else
- return 5;
- }
- if (weight[6] + weight[7] + weight[8] == twoWeights) {
- if (weight[6] == 0)
- return 6;
- else if (weight[7] == 0)
- return 7;
- else
- return 8;
- }
- if (weight[0] + weight[3] + weight[6] == twoWeights) {
- if (weight[0] == 0)
- return 0;
- else if (weight[3] == 0)
- return 3;
- else
- return 6;
- }
- if (weight[1] + weight[4] + weight[7] == twoWeights) {
- if (weight[1] == 0)
- return 1;
- else if (weight[4] == 0)
- return 4;
- else
- return 7;
- }
- if (weight[2] + weight[5] + weight[8] == twoWeights) {
- if (weight[2] == 0)
- return 2;
- else if (weight[5] == 0)
- return 5;
- else
- return 8;
- }
- if (weight[2] + weight[4] + weight[6] == twoWeights) {
- if (weight[2] == 0)
- return 2;
- else if (weight[4] == 0)
- return 4;
- else
- return 6;
- }
- if (weight[0] + weight[4] + weight[8] == twoWeights) {
- if (weight[0] == 0)
- return 0;
- else if (weight[4] == 0)
- return 4;
- else
- return 8;
- }
- return -1;
- }
- int getRandomSquare() {
- boolean gotEmptySquare = false;
- int selectedSquare = -1;
- do {
- selectedSquare=(int) (Math.random()*9);
- if (squares[selectedSquare].getLabel().equals("")) {
- gotEmptySquare = true;
- }
- } while (!gotEmptySquare );
- return selectedSquare;
- }
- void highlightWinner (int win1, int win2, int win3) {
- squares[win1].setBackground(Color.CYAN);
- squares[win2].setBackground(Color.CYAN);
- squares[win3].setBackground(Color.CYAN);
- }
- void endTheGame () {
- newGameButton.setEnabled(true);
- for (int i=0; i<9; i++) {
- squares[i].setEnabled(false);
- }
- JFrame frame = new JFrame("TicTac");
- frame.setContentPane(windowContent);
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- TicTac TicTac =new TicTac();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement