import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Random;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Spring;
import javax.swing.border.TitledBorder;
public class DiceMain extends JApplet implements MouseListener, ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
static int [][] pole = new int[14][14];// Массив принадлежности территории, тому или другому игроку.
static String[][] polez = new String[14][14];// Массив с кол-м кубиков.
int k1, k2, k3;
int x, y , x1, y1;
Random r = new Random();
static Graphics2D g, gg;
static Graphics gr;
MouseEvent me;
int player1, player2;//у игрока2 pole =1.
JButton buthod, butend;
JPanel panel;
public void init()
{
setLayout(new BorderLayout());
setSize(400, 500);
setMinimumSize(new Dimension (400,500));
buthod = new JButton("Начать заново");
butend = new JButton("Закончить ход");
panel = new JPanel();
panel.setBorder(new TitledBorder("Действия"));
panel.add(buthod);
panel.add(butend);
//panel.setPreferredSize(new Dimension(300,100));
add(panel,BorderLayout.SOUTH);
for(int i=0; i<14;i++)
for(int j=0; j<14;j++)
{
polez[i][j] = String.valueOf(r.nextInt(4)+1);
pole[i][j] = r.nextInt(2);
}
addMouseListener(this);
buthod.addActionListener(this);
butend.addActionListener(this);
}
public void paint(Graphics gr) {
g = (Graphics2D)gr;
gg = (Graphics2D)gr;
super.paint(gr);
//super.paint(gg);
for(int l=0; l<14;l++)
for(int m=0; m<14;m++)
{
int i = l*25;
int j = m*25;
if(pole[l][m]==1)//игрок2
{
g.setColor(Color.MAGENTA);
g.fill(new Rectangle2D.Float(24+i, 24+j, 24, 24));
}
if(pole[l][m]==0)//игрок1
{
g.setColor(Color.GREEN);
g.fill(new Rectangle2D.Float(24+i, 24+j, 24, 24));
}
String f = polez[l][m];
g.setColor(Color.BLACK);
gr.drawString(f,i+31,j+39);
}
if(x!=0)
for(int i=1; i<15;i++)
if(x<=25*(i+1) && x>=25*(i))
for(int j=1; j<15;j++)
if(y<=25*(j+1) && y>=25*(j))
if(pole[i-1][j-1]!=1)
{
gg.setColor(Color.DARK_GRAY);
gg.fill(new Rectangle2D.Float(25*i-1, 25*j-1, 24, 24));
System.out.println(x+" "+y +" ");
k1 = 25*i-1;
k2 = 25*j-1;
String f = polez[i-1][j-1];
gg.setColor(Color.WHITE);
gr.drawString(f,25*(i-1)+30,25*(j-1)+38);
pole[i-1][j-1]=2;
x=0;
}
if(x1!=0)
if(x1>(k1+12-37) && x1<(k1+12+37) && y1>(k2+12-37) && y1<(k2+12+37))
for(int i=1; i<15;i++)
if(x1<=25*(i+1) && x1>=25*i)
for(int j=1; j<15;j++)
if(y1<=25*(j+1) && y1>=25*j)
if(pole[i-1][j-1]!=0)
{
gg.setColor(Color.BLUE);
gg.fill(new Rectangle2D.Float(25*i-1, 25*j-1, 24, 24));
System.out.println(x1+" "+y1 +" ");
String f = polez[i-1][j-1];
gg.setColor(Color.WHITE);
gr.drawString(f,25*(i-1)+30,25*(j-1)+38);
pole[i-1][j-1]=3;
x1=0;
for(int i1=0; i1<14;i1++)
for(int j1=0; j1<14;j1++)
if(pole[i1][j1]==2)
{
player1 = Integer.parseInt(polez[i1][j1]);//*(r.nextInt(5)+1);
for(int l=0; l<14;l++)
for(int m=0; m<14;m++)
if(pole[l][m]==3)
{
player2 = Integer.parseInt(polez[l][m]);//*(r.nextInt(5)+1);
if(player1>player2)
{
pole[i1][j1] = 0;
pole[l][m] = 0;
polez[l][m] = String.valueOf((Integer.parseInt(polez[i1][j1])-1));
polez[i1][j1] = String.valueOf(1);
repaint();
System.out.println(polez[l][m]+"player1 больше");
}
else
{
pole[i1][j1] = 0;
pole[l][m] = 1;
polez[i1][j1] = String.valueOf(1);
repaint();
System.out.println(pole[i1][j1]+ "bot больше");
}
}
}
}
}
public void paintComponents(Graphics g) {super.paintComponents(g);}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()== buthod){}
if (ae.getSource()== butend)//Бот+добавление очков
{
for(int i=0; i<14;i++)
for(int j=0; j<14;j++)
if(pole[i][j]==0)
{
if(Integer.parseInt(polez[i][j])!=4)
polez[i][j]=String.valueOf(Integer.parseInt(polez[i][j])+1);
}
for(int i=0; i<14;i++)
for(int j=0; j<14;j++)
if(pole[i][j]==1)
{
if(i!=0)
{
if(pole[i-1][j]==0)
if(Integer.parseInt(polez[i][j])>Integer.parseInt(polez[i-1][j]))
{
pole[i-1][j]=1;
polez[i][j]=polez[i-1][j];
repaint();
}
if(j!=0)
if(pole[i-1][j-1]==0)
if(Integer.parseInt(polez[i][j])>Integer.parseInt(polez[i-1][j-1]))
{
pole[i-1][j-1]=1;
polez[i][j]=polez[i-1][j-1];
repaint();
}
if(j!=13)
if(pole[i-1][j+1]==0)
if(Integer.parseInt(polez[i][j])>Integer.parseInt(polez[i-1][j+1]))
{
pole[i-1][j+1]=1;
polez[i][j]=polez[i-1][j+1];
repaint();
}
}
if(i!=13)
{
if(pole[i+1][j]==0)
if(Integer.parseInt(polez[i][j])>Integer.parseInt(polez[i+1][j]))
{
pole[i+1][j]=1;
polez[i][j]=polez[i+1][j];
repaint();
}
if(j!=13)
if(pole[i+1][j+1]==0)
if(Integer.parseInt(polez[i][j])>Integer.parseInt(polez[i+1][j+1]))
{
pole[i+1][j+1]=1;
polez[i][j]=polez[i+1][j+1];
repaint();
}
}
if(j!=13)
if(pole[i][j+1]==0)
if(Integer.parseInt(polez[i][j])>Integer.parseInt(polez[i][j+1]))
{
pole[i][j+1]=1;
polez[i][j]=polez[i][j+1];
repaint();
}
if(j!=0)
{
if(i!=13)
if(pole[i+1][j-1]==0)
if(Integer.parseInt(polez[i][j])>Integer.parseInt(polez[i+1][j-1]))
{
pole[i+1][j-1]=1;
polez[i][j]=polez[i+1][j-1];
repaint();
}
if(pole[i][j-1]==0)
if(Integer.parseInt(polez[i][j])>Integer.parseInt(polez[i][j-1]))
{
pole[i][j-1]=1;
polez[i][j]=polez[i][j-1];
repaint();
}
}
if(Integer.parseInt(polez[i][j])!=4)
polez[i][j]=String.valueOf(Integer.parseInt(polez[i][j])+1);
}
}
}
public void mouseClicked(MouseEvent me)
{
if(me.getButton()==MouseEvent.BUTTON1)
{
x = me.getX();
y = me.getY();
if(x>=24 && y>=24)
repaint();
}
if(me.getButton()==MouseEvent.BUTTON3)
{
x1 = me.getX();
y1 = me.getY();
if(x1>=24 && y1>=24)
repaint();
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}