import javax.swing.*;
import java.awt.*;
import java.sql.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.Connection;
import java.text.*;
import java.util.*;
import java.util.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Listener extends MouseAdapter implements ActionListener {
Graphics g;
Chess ch;
int x1,x2,y1,y2,pedometer = 0;
int r=-1,c=-1;
int [][]record = new int[9999][6];
int allflag = 0;
private final static String uri = "jdbc:mysql://localhost:3306/nn?useSSL=true";
private final static String user = "root";
private final static String password = "";
Statement stmt;
Connection con;
ResultSet rs;
String listname;
JTextField ter,teb;
int Dateflag = 0;
String nowdate;
JComboBox<String> combo;
int replayflag = 0,replayflag1=0;
int [][]replaycord;
String replayDate;
int yxz,zyh;
public void setJTextField(JTextField ter,JTextField teb) {
this.ter = ter;
this.teb = teb;
}
public void setJComboBox(JComboBox<String> combo) {
this.combo = combo;
}
public void updaterecord() {
record[pedometer][0] = beforechess[0];
record[pedometer][1] = beforechess[1];
record[pedometer][2] = beforechess[2];
record[pedometer][3] = r;
record[pedometer][4] = c;
record[pedometer][5] = location[r][c];
if(Dateflag == 1) {
Date date = new Date();
nowdate = date.toString();
Dateflag = 0;
}
String s1 = record[pedometer][0] + " ";
String s2 = record[pedometer][1] + " ";
String s3 = record[pedometer][2] + " ";
String s4 = record[pedometer][3] + " ";
String s5 = record[pedometer][4] + " ";
String s6 = record[pedometer][5] + " ";
listname = "('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+nowdate+"','"+pedometer+"')";
System.out.println(nowdate);
String sqlStr = "INSERT INTO replay001(beforechess0,beforechess1,beforechess2,nowchess0,nowchess1,location,Date,number) VALUES" +listname;
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception h) {
h.printStackTrace();
}
try {
con = DriverManager.getConnection(uri, user, password);
stmt = con.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(Exception h) {
System.out.println("记录中不能重复"+h);
}
pedometer++;
}
public void regret() {
r=-1;
if(pedometer>0) {
location[record[pedometer-1][0]][record[pedometer-1][1]] = record[pedometer-1][2];
location[record[pedometer-1][3]][record[pedometer-1][4]] = record[pedometer-1][5];
rechessflag();
pedometer--;
String sqlStr="delete from replay001 where Date='"+nowdate+"' and number="+pedometer;
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception h) {
h.printStackTrace();
}
try {
con = DriverManager.getConnection(uri, user, password);
stmt = con.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(Exception h) {
System.out.println("记录中不能重复"+h);
}
}
}
public void restart() {
location = new int[][] {{1,2,3,4,5,4,3,2,1},{0,0,0,0,0,0,0,0,0},{0,6,0,0,0,0,0,6,0},{7,0,7,0,7,0,7,0,7},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{77,0,77,0,77,0,77,0,77},{0,66,0,0,0,0,0,66,0},{0,0,0,0,0,0,0,0,0},{11,22,33,44,55,44,33,22,11}};
nowchess = new int[3];
beforechess = new int[3];
r=-1;
c=-1;
record = new int[9999][6];
chessflag = 1;
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if(s.equals("开始游戏")) {
restart();
ch.repaint();
allflag = 1;
ch.la.setText("开始!!!");
Dateflag = 1;
}
else if(s.equals("悔棋")) {
regret();
ch.repaint();
}
else if(s.equals("重新开始")){
restart();
ch.repaint();
allflag = 0;
Dateflag = 1;
}
else if(s.equals("记录")) {
listname = "('"+ter.getText()+"','"+teb.getText()+"')";
String sqlStr = "INSERT INTO match11(red,black) VALUES" +listname;
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception h) {
h.printStackTrace();
}
try {
con = DriverManager.getConnection(uri, user, password);
stmt = con.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(Exception h) {
System.out.println("记录中不能重复"+h);
}
}
else if(s.equals("开始复盘")) {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception h) {
h.printStackTrace();
}
try {
con = DriverManager.getConnection(uri, user, password);
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM replay001");
replaycord = new int[9999][6];
yxz = 0;
while(rs.next()) {
if(rs.getString(7).equals(combo.getSelectedItem().toString().trim())) {
replaycord[yxz][0] = Integer.parseInt(rs.getString(1));
replaycord[yxz][1] = Integer.parseInt(rs.getString(2));
replaycord[yxz][2] = Integer.parseInt(rs.getString(3));
replaycord[yxz][3] = Integer.parseInt(rs.getString(4));
replaycord[yxz][4] = Integer.parseInt(rs.getString(5));
replaycord[yxz][5] = Integer.parseInt(rs.getString(6));
yxz++;
}
}
replayflag = 1;
zyh = 0;
System.out.println("yxz:"+yxz);
}
catch(Exception h) {
h.printStackTrace();
}
}
else if(s.equals("下一步")) {
if(replayflag ==1) {
if(zyh<yxz) {
location[replaycord[zyh][3]][replaycord[zyh][4]] = replaycord[zyh][2];
location[replaycord[zyh][0]][replaycord[zyh][1]] = 0;
ch.repaint();
zyh++;
}
}
}
else if(s.equals("上一步")) {
if(replayflag ==1) {
if(zyh>0) {
location[replaycord[zyh-1][0]][replaycord[zyh-1][1]] = replaycord[zyh-1][2];
location[replaycord[zyh-1][3]][replaycord[zyh-1][4]] = replaycord[zyh-1][5];
ch.repaint();
zyh--;
}
}
}
}
public void win() {
if(chessflag==1&&nowchess[2] ==5) {
ch.la.setText("红方胜!!!");
listname = "('"+ter.getText()+"')";
String sqlStr = "INSERT INTO match11(winner) VALUES" +listname;
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception h) {
h.printStackTrace();
}
try {
con = DriverManager.getConnection(uri, user, password);
stmt = con.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(Exception h) {
System.out.println("记录中不能重复"+h);
}
allflag = 0;
}
else if(chessflag ==2&&nowchess[2]==55) {
ch.la.setText("黑方胜!!!");
listname = "('"+teb.getText()+"')";
String sqlStr = "INSERT INTO match11(winner) VALUES" +listname;
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception h) {
h.printStackTrace();
}
try {
con = DriverManager.getConnection(uri, user, password);
stmt = con.createStatement();
stmt.executeUpdate(sqlStr);
}
catch(Exception h) {
System.out.println("记录中不能重复"+h);
}
allflag = 0;
}
}
public void setG(Graphics g) {
this.g = g;
}
public void setChess(Chess ch) {
this.ch = ch;
}
int [][] location = new int[][] {{1,2,3,4,5,4,3,2,1},{0,0,0,0,0,0,0,0,0},{0,6,0,0,0,0,0,6,0},{7,0,7,0,7,0,7,0,7},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{77,0,77,0,77,0,77,0