Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.io.*;
- class Model{
- char tab[][] = new char[10][10];
- }
- public class GIdoOTHELLO extends JFrame{
- int wciskk=1;
- Model model = new Model();
- /*DEKLARACJA PANELI i PODZIAL NA PANEL WYSWIETLAJACY PLANSZE, PANEL STERUJACY oraz PANEL TEKSTOWY*/
- JPanel plansza = new JPanel();
- JPanel menu = new JPanel();
- JTextArea tekst = new JTextArea();
- /*DEKLARACJA PRZYCISKOW*/
- JButton help = new JButton("Pomoc");
- JButton prev = new JButton("Cofnij");
- JButton next = new JButton("Dalej");
- JButton tab[][] = new JButton[12][12];//tablica planszy
- JButton load = new JButton("Wczytaj");
- JButton save = new JButton("Zapisz");
- JButton bialy = new JButton("Bialy");
- JButton czarny = new JButton("Czarny");
- JButton koniec = new JButton("Sprawdz");
- /*DEKLARACJA POZOSTALYCH ELEMENTOW*/
- JTextField blad = new JTextField(20);
- JTextField name = new JTextField(10);
- public FINISH finish = new FINISH();
- public GIdoOTHELLO(){
- /*NADANIE TYTULU*/
- setTitle("OTHELLO stworzone przez Michal Gorski 224689");
- /*DEKLARACJA KONTENERA*/
- Container ct = getContentPane();
- ct.setLayout(new GridLayout(1,3));
- ct.add(plansza);
- ct.add(menu);
- ct.add(tekst);
- /*WYPELNIENIE PLANSZY*/
- int i,j;
- plansza.setLayout(new GridLayout(10,10));
- for( i=1;i<11;i++){
- for( j=1;j<11;j++){
- tab[i][j] = new JButton("");
- if((i+j) % 2 == 0){
- (tab[i][j]).setBackground(Color.magenta);
- }else{
- (tab[i][j]).setBackground(Color.magenta);
- }
- plansza.add(tab[i][j]);
- (tab[i][j]).addActionListener(new ZAZNACZ(i,j));
- }
- }
- /*WYPELNIENIE BOKOW*/
- for(i=0;i<11;i++){
- tab[i][0]= new JButton("");
- (tab[i][0]).setBackground(Color.magenta);
- }
- for(j=0;j<11;j++){
- tab[0][j]= new JButton("");
- (tab[0][j]).setBackground(Color.magenta);
- }
- for(i=0;i<11;i++){
- tab[i][11]= new JButton("");
- (tab[i][11]).setBackground(Color.magenta);
- }
- for(j=0;j<11;j++){
- tab[11][j]= new JButton("");
- (tab[11][j]).setBackground(Color.magenta);
- }
- (tab[5][1]).setBackground(Color.black);
- (tab[5][2]).setBackground(Color.black);
- (tab[4][3]).setBackground(Color.black);
- (tab[4][4]).setBackground(Color.black);
- (tab[4][5]).setBackground(Color.black);
- (tab[6][5]).setBackground(Color.black);
- (tab[8][6]).setBackground(Color.black);
- (tab[5][7]).setBackground(Color.black);
- (tab[4][9]).setBackground(Color.black);
- (tab[4][10]).setBackground(Color.black);
- (tab[2][3]).setBackground(Color.white);
- (tab[2][7]).setBackground(Color.white);
- (tab[3][5]).setBackground(Color.white);
- (tab[4][7]).setBackground(Color.white);
- (tab[6][3]).setBackground(Color.white);
- (tab[6][7]).setBackground(Color.white);
- (tab[6][9]).setBackground(Color.white);
- (tab[7][1]).setBackground(Color.white);
- (tab[7][4]).setBackground(Color.white);
- (tab[7][8]).setBackground(Color.white);
- (tab[8][2]).setBackground(Color.white);
- (tab[8][3]).setBackground(Color.white);
- (tab[8][8]).setBackground(Color.white);
- (tab[9][7]).setBackground(Color.white);
- (tab[9][10]).setBackground(Color.white);
- /*WYPELNIENIE MENU*/
- menu.setLayout(new GridLayout(5,2));
- menu.add(help);
- menu.add(blad);
- menu.add(prev);
- menu.add(next);
- menu.add(czarny);
- menu.add(bialy);
- menu.add(koniec);
- menu.add(name);
- menu.add(load);
- menu.add(save);
- prev.addActionListener(new COFNIJ());
- next.addActionListener(new NASTEPNY());
- save.addActionListener(new ZAPISZ());
- load.addActionListener(new WCZYTAJ());
- help.addActionListener(new POMOC());
- czarny.addActionListener(new WCISKc());
- bialy.addActionListener(new WCISKb());
- koniec.addActionListener(new KONIEC());
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public class WCISKb implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- if(wciskk==0){
- wciskk++;
- }
- else if (wciskk!=0) {
- wciskk=1;
- blad.setText("Kolor przycisku zostal zrestartowany!");
- }
- }
- }
- public class WCISKc implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- if(wciskk==1){
- wciskk--;
- }
- else if (wciskk!=1) {
- wciskk=1;
- blad.setText("Kolor przycisku zostal zrestartowany!");
- }
- }
- }
- public class ZAZNACZ implements ActionListener{
- int i,j;
- ZAZNACZ(int i, int j){this.i=i;this.j=j;}
- public void actionPerformed(ActionEvent e){
- if((tab[i][j]).getBackground()==Color.magenta && wciskk==0) {
- if(((tab[i-1][j-1]).getBackground()==Color.black && (tab[i-1][j]).getBackground()==Color.black && (tab[i][j-1]).getBackground()==Color.black)||((tab[i-1][j]).getBackground()==Color.black && (tab[i-1][j+1]).getBackground()==Color.black && (tab[i][j+1]).getBackground()==Color.black)||((tab[i][j+1]).getBackground()==Color.black && (tab[i+1][j+1]).getBackground()==Color.black && (tab[i+1][j]).getBackground()==Color.black)||((tab[i+1][j]).getBackground()==Color.black && (tab[i+1][j-1]).getBackground()==Color.black && (tab[i][j-1]).getBackground()==Color.black)) {
- blad.setText("BLAD! NIEDOZWOLONY RUCH!");
- }
- else{
- (tab[i][j]).setBackground(Color.black);
- finish.dodajRuch(i,j,"black");
- }
- }
- else if ((tab[i][j]).getBackground()==Color.magenta && wciskk==1) {
- if(((tab[i-1][j-1]).getBackground()==Color.white && (tab[i-1][j]).getBackground()==Color.white && (tab[i][j-1]).getBackground()==Color.white)||((tab[i-1][j]).getBackground()==Color.white && (tab[i-1][j+1]).getBackground()==Color.white && (tab[i][j+1]).getBackground()==Color.white)||((tab[i][j+1]).getBackground()==Color.white && (tab[i+1][j+1]).getBackground()==Color.white && (tab[i+1][j]).getBackground()==Color.white)||((tab[i+1][j]).getBackground()==Color.white && (tab[i+1][j-1]).getBackground()==Color.white && (tab[i][j-1]).getBackground()==Color.white)) {
- blad.setText("BLAD! NIEDOZWOLONY RUCH!");
- }
- else{
- (tab[i][j]).setBackground(Color.white);
- finish.dodajRuch(i,j,"white");
- }
- }
- else{
- blad.setText("BLAD! NIEDOZWOLONY RUCH!");
- }
- }
- }
- public class POMOC implements ActionListener{
- public void actionPerformed(ActionEvent e){
- tekst.setText("OTHELLO 1.02 by Michal Gorski nr indeksu 224689"+"\n"+"Witaj w grze OTHELLO! Zasady gry polegaja na tym, aby uzupelnic cala plansze"+"\n"+" czarnymi i bialymi kolorami tak, aby nie tworzyc tego samego koloru w postaci 2x2,"+"\n"+" pamietajac przy tym by wszystkie te same kolory byly ze soba polaczone."+"\n"+"Kilka uwag:"+"\n"+"1.Poczatkowo zaznaczony kolor to zawsze bialy."+"\n"+"2.Aby zapisac gre nalezy wpisac nazwe zapisu do pustego pola i nacisnac zapisz."+"\n"+"3.Wczytywanie dziala analogicznie do zapisywania"+"\n"+"Powodzenia i udanej zabawy! =)");
- }
- }
- /*ZAPIS*/
- public class ZAPISZ implements ActionListener{
- public void actionPerformed(ActionEvent e){
- String nazwa = name.getText();
- File zapis = new File(nazwa+".ser");
- String warn;
- try {
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:\\Users\\DajMiNazwe\\Desktop\\Java\\Projekt\\"+nazwa+".ser"));
- out.writeObject(finish);
- out.close();
- blad.setText("Udalo sie zapisac stan GRY!");
- } catch (IOException e1) {
- blad.setText("Blad zapisu do pliku!!");
- }
- }
- }//ZAPISZ
- public class COFNIJ implements ActionListener{
- public void actionPerformed(ActionEvent e){
- /*Jesli biezacy indeks 'ruchu' to poczatek listy ruchow to...*/
- if(finish.biezacyIndeks==0){
- /*blokuj przycisk prev*/
- prev.setEnabled(false);
- /*odblokuj przycisk next*/
- next.setEnabled(true);
- /*powiadom gracza o osiagnieciu limitu posuniec do tylu*/
- blad.setText("Wykonano maksymalna liczbe posuniec do tylu!");
- }else{
- /*wyczysc pole tekstu*/
- blad.setText(null);
- /*odblokuj przycisk next*/
- next.setEnabled(true);
- /*pobierz dane o ostatnim ruchu*/
- RUCH cofany = finish.cofnijRuch();
- /*wyczysc przycisk (domyslnie z ikony)*/
- if((tab[cofany.x][cofany.y]).getBackground()!=Color.magenta){
- (tab[cofany.x][cofany.y]).setBackground(Color.magenta);
- (tab[cofany.x][cofany.y]).setEnabled(true);
- model.tab[cofany.x][cofany.y]=' ';
- }
- /*przywroc kolor na czarny*/
- }
- }
- }//COFNIJ
- /*NASTEPNY RUCH (przycisk DALEJ)*/
- public class NASTEPNY implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if(finish.biezacyIndeks==finish.ruchy.size()){
- blad.setText("Wykonano maksymalna liczbe posuniec do przodu!");
- }else{
- blad.setText(null);
- prev.setEnabled(true);
- RUCH nastepny = finish.nastepnyRuch();
- if((nastepny.kolor).equals("white")){
- (tab[nastepny.x][nastepny.y]).setBackground(Color.white);
- }else{
- (tab[nastepny.x][nastepny.y]).setBackground(Color.black);
- }
- }
- }
- }//NASTEPNY
- public class KONIEC implements ActionListener{
- int i=1,j=1;
- public void actionPerformed(ActionEvent e) {
- while(tab[i][j].getBackground()!=Color.magenta) {
- if(tab[i][j].getBackground()==Color.white && (tab[i-1][j].getBackground()==Color.white || tab[i][j+1].getBackground()==Color.white || tab[i+1][j].getBackground()==Color.white || tab[i][j-1].getBackground()==Color.white)) {
- blad.setText("OK!");
- i++;
- j++;
- }
- else if(tab[i][j].getBackground()==Color.black && (tab[i-1][j].getBackground()==Color.black || tab[i][j+1].getBackground()==Color.black || tab[i+1][j].getBackground()==Color.black || tab[i][j-1].getBackground()==Color.black)) {
- i++;
- j++;
- }
- else
- blad.setText("TO NIE KONIEC GRY!");
- break;
- }
- }
- }//KONIEC
- /*KLASA OPISUJACA WCZYTYWANIE STANU GRY*/
- public class WCZYTAJ implements ActionListener{
- public void actionPerformed(ActionEvent e){
- String plik = name.getText();
- finish = finish.wczytajRuchy(plik);
- for(int i=0;i<finish.ruchy.size();i++){
- RUCH bierzacy = finish.ruchy.get(i);
- }
- for(int i=0;i<finish.ruchy.size();i++){
- RUCH bierzacy = finish.ruchy.get(i);
- if((tab[bierzacy.x][bierzacy.y]).getBackground()==Color.black){
- // blad.setText("Wykonano maksymalna liczbe posuniec do przodu!");
- (tab[bierzacy.x][bierzacy.y]).setBackground(Color.black);
- }
- if((tab[bierzacy.x][bierzacy.y]).getBackground()==Color.white){
- (tab[bierzacy.x][bierzacy.y]).setBackground(Color.white);
- blad.setText("Wykonano maksymalna liczbe posuniec do przodu!");
- }
- }
- }
- }//WCZYTAJ
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement