上周花了一周时间做的课程设计的项目,实现的功能如下:
/** * 简单模式自动下棋的电脑 * * @param x * @param y 玩家下的坐标 * @param Color 玩家下的棋的颜色 黑1白2 * * x&y这里要保存已经下过的点位,并判断是否已经下过,难点是在于如果下的位置在角落处(或周围已经没有棋子可以自动生成位置了,这时要递归判断并随机生成一个新的位置,直到全图都没有位置了为止终止递归):*/ private void naocanautomatic(int x, int y, int Color) { int[][] temp = {{x - 1, y - 1}, {x, y - 1}, {x + 1, y - 1}, {x - 1, y}, {x + 1, y}, {x - 1, y + 1}, {x, y + 1}, {x + 1, y + 1}}; ArrayList<int[]> templist = new ArrayList<>(); for (int i = 0; i < temp.length; i++) { if (temp[i][0] >= 0 && temp[i][0] < 13 && temp[i][1] >= 0 && temp[i][1] < 13) { templist.add(temp[i]); } } //判断是否已经下过 panduanshifouyijingxiaguo(templist); int num = (int) (Math.random() * templist.size()); int a = templist.get(num)[0]; int b = templist.get(num)[1]; putChess(a, b, Color); }
/** * 递归判断是否已经下过 * * @param templist */ private void panduanshifouyijingxiaguo(ArrayList<int[]> templist) { for (int i = 0; i < storageHadChess.size(); i++) { //如有重复,则删掉 for (int j = 0; j < templist.size(); j++) { if (storageHadChess.get(i)[0] == templist.get(j)[0] && storageHadChess.get(i)[1] == templist.get(j)[1]) { templist.remove(j); //递归防止周围没有字落下时直接崩掉 if (templist.size() == 0) { templist.add(new int[]{(int) (Math.random() * (GRID_SIZE - 2)), (int) (Math.random() * (GRID_SIZE - 2))}); // Log.d("whalea", " " + (int) (Math.random() * (GRID_SIZE - 2))); panduanshifouyijingxiaguo(templist); } } } } }2、普通难度算法实现:
/** * 普通模式自动下棋的电脑 * 黑1白2 * * @param x * @param y * @param Color */ private void normalautomatic(int x, int y, int Color) { int duishouColor = 0;//对手的颜色 //根据我方颜色推测出对手颜色 if (Color == 1) { duishouColor = 2; } else { duishouColor = 1; } //判断我方是否有3个连成一线了 for (int i = 0; i < GRID_SIZE - 1; i++) //i表示列(根据宽度算出来的) for (int j = 0; j < GRID_SIZE - 1; j++) { //i表示行(根据高度算出来的) //检测横轴三个相连 if ((((i + 3) < (GRID_SIZE - 1)) && (mGridArray[i][j] == Color) && (mGridArray[i + 1][j] == Color) && (mGridArray[i + 2][j] == Color)) || (((i + 3) < (GRID_SIZE - 1)) && (mGridArray[i][j] == duishouColor) && (mGridArray[i + 1][j] == duishouColor) && (mGridArray[i + 2][j] == duishouColor))) { //如果有三个点相连了 //先判断是否已经测试过这三个点 boolean aa = false; for (int p = 0; p < cunchusandianArraylist.size(); p++) { String sandiantemp = cunchusandianArraylist.get(p); String[] sandiantemps = sandiantemp.split(":"); //如果这三个点已经存在 if ((Integer.parseInt(sandiantemps[0]) == i) && (Integer.parseInt(sandiantemps[1]) == j) && (Integer.parseInt(sandiantemps[2]) == (i + 1)) && (Integer.parseInt(sandiantemps[3]) == j) && (Integer.parseInt(sandiantemps[4]) == (i + 2)) && (Integer.parseInt(sandiantemps[5]) == j)) { aa = true; } } if (aa == true) { } else { //在两边端点位置随机下一个 ifsangedianxianglian(i - 1, j, i + 3, j, Color); cunchusandianArraylist.add(i + ":" + j + ":" + (i + 1) + ":" + j + ":" + (i + 2) + ":" + j); return; } } //纵轴3个相连 if ((((j + 3) < (GRID_SIZE - 1)) && (mGridArray[i][j] == Color) && (mGridArray[i][j + 1] == Color) && (mGridArray[i][j + 2] == Color)) || (((j + 3) < (GRID_SIZE - 1)) && (mGridArray[i][j] == duishouColor) && (mGridArray[i][j + 1] == duishouColor) && (mGridArray[i][j + 2] == duishouColor))) { //如果有三个点相连了 //先判断是否已经测试过这三个点 boolean aa = false; for (int p = 0; p < cunchusandianArraylist.size(); p++) { String sandiantemp = cunchusandianArraylist.get(p); String[] sandiantemps = sandiantemp.split(":"); if ((Integer.parseInt(sandiantemps[0]) == i) && (Integer.parseInt(sandiantemps[1]) == j) && (Integer.parseInt(sandiantemps[2]) == i) && (Integer.parseInt(sandiantemps[3]) == (j + 1)) && (Integer.parseInt(sandiantemps[4]) == i) && (Integer.parseInt(sandiantemps[5]) == (j + 2))) { aa = true; } } if (aa == true) { } else { //在两边端点位置随机下一个 ifsangedianxianglian(i, j - 1, i, j + 3, Color); cunchusandianArraylist.add(i + ":" + j + ":" + i + ":" + (j + 1) + ":" + i + ":" + (j + 2)); return; } } //左上到右下3个相连 if ((((j + 3) < (GRID_SIZE - 1)) && ((i + 3) < (GRID_SIZE - 1)) && (mGridArray[i][j] == Color) && (mGridArray[i + 1][j + 1] == Color) && (mGridArray[i + 2][j + 2] == Color)) || (((j + 3) < (GRID_SIZE - 1)) && ((i + 3) < (GRID_SIZE - 1)) && (mGridArray[i][j] == duishouColor) && (mGridArray[i + 1][j + 1] == duishouColor) && (mGridArray[i + 2][j + 2] == duishouColor))) { //如果有三个点相连了 //先判断是否已经测试过这三个点 boolean aa = false; for (int p = 0; p < cunchusandianArraylist.size(); p++) { String sandiantemp = cunchusandianArraylist.get(p); String[] sandiantemps = sandiantemp.split(":"); if ((Integer.parseInt(sandiantemps[0]) == i) && (Integer.parseInt(sandiantemps[1]) == j) && (Integer.parseInt(sandiantemps[2]) == (i + 1)) && (Integer.parseInt(sandiantemps[3]) == (j + 1)) && (Integer.parseInt(sandiantemps[4]) == (i + 2)) && (Integer.parseInt(sandiantemps[5]) == (j + 2))) { aa = true; } } if (aa == true) { } else { ifsangedianxianglian(i - 1, j - 1, i + 3, j + 3, Color); cunchusandianArraylist.add(i + ":" + j + ":" + (i + 1) + ":" + (j + 1) + ":" + (i + 2) + ":" + (j + 2)); return; } } //右上到左下3个相连 if ((((i - 3) >= 0) && ((j + 3) < (GRID_SIZE - 1)) && (mGridArray[i][j] == Color) && (mGridArray[i - 1][j + 1] == Color) && (mGridArray[i - 2][j + 2] == Color)) || (((i - 3) >= 0) && ((j + 3) < (GRID_SIZE - 1)) && (mGridArray[i][j] == duishouColor) && (mGridArray[i - 1][j + 1] == duishouColor) && (mGridArray[i - 2][j + 2] == duishouColor))) { //如果有三个点相连了 //先判断是否已经测试过这三个点 boolean aa = false; for (int p = 0; p < cunchusandianArraylist.size(); p++) { String sandiantemp = cunchusandianArraylist.get(p); String[] sandiantemps = sandiantemp.split(":"); if ((Integer.parseInt(sandiantemps[0]) == i) && (Integer.parseInt(sandiantemps[1]) == j) && (Integer.parseInt(sandiantemps[2]) == (i - 1)) && (Integer.parseInt(sandiantemps[3]) == (j + 1)) && (Integer.parseInt(sandiantemps[4]) == (i - 2)) && (Integer.parseInt(sandiantemps[5]) == (j + 2))) { aa = true; } } if (aa == true) { } else { ifsangedianxianglian(i + 1, j - 1, i - 3, j + 3, Color); cunchusandianArraylist.add(i + ":" + j + ":" + (i - 1) + ":" + j + 1 + ":" + (i - 2) + ":" + (j + 2)); return; } } } int[][] temp = {{x - 1, y - 1}, {x, y - 1}, {x + 1, y - 1}, {x - 1, y}, {x + 1, y}, {x - 1, y + 1}, {x, y + 1}, {x + 1, y + 1}}; ArrayList<int[]> templist = new ArrayList<>(); for (int k = 0; k < temp.length; k++) { if (temp[k][0] >= 0 && temp[k][0] < 13 && temp[k][1] >= 0 && temp[k][1] < 13) { templist.add(temp[k]); } //判断是否已经下过 panduanshifouyijingxiaguo(templist); int num = (int) (Math.random() * templist.size()); int a = templist.get(num)[0]; int b = templist.get(num)[1]; putChess(a, b, Color); return; } }
//接收广播 /** * 接受广播,并显示尚未配对的可用的周围所有蓝牙设备 */ private class BluetoothReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //如果是正在扫描状态 if (BluetoothDevice.ACTION_FOUND.equals(action)) { //只要BluetoothReceiver接收到来自于系统的广播,这个广播是什么呢,是我找到了一个远程蓝牙设备 //Intent代表刚刚发现远程蓝牙设备适配器的对象,可以从收到的Intent对象取出一些信息 BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 如果该设备已经被配对,则跳过 // if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDED) { if (!devices.contains(bluetoothDevice)) { //设备数组获得新的设备信息并更新adapter deviceNameAndDresss.add(new Device(bluetoothDevice.getName(), bluetoothDevice.getAddress(),bluetoothDevice.getBondState())); //添加新的设备到设备Arraylist devices.add(bluetoothDevice); deviceshowAdapter.notifyDataSetChanged(); } } } }也可以调用BlueToothAapter的startDiscovery()方法主动进行扫描:
//扫描周围的蓝牙设备按钮监听器 private class SaoMiaoButtonListener implements View.OnClickListener { @Override public void onClick(View v) { ObjectAnimator animator = ObjectAnimator.ofFloat(v,"rotation",0,359); animator.setRepeatCount(12); animator.setDuration(1000); animator.start(); isQuering = true; Toast.makeText(BlueToothFindOthersAty.this, "开始扫描", Toast.LENGTH_SHORT).show(); //清空列表 deviceNameAndDresss.clear(); //获得已配对的蓝牙设备 Set服务端等待连接模块:pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if (!devices.contains(device)) { deviceNameAndDresss.add(new Device(device.getName(), device.getAddress(),device.getBondState())); devices.add(device); } } } deviceshowAdapter.setDevices(deviceNameAndDresss); deviceshowAdapter.notifyDataSetChanged(); //开始扫描周围的可见的蓝牙设备 bluetoothAdapter.startDiscovery(); } }
//开启子线程等待连接 new Thread(new Runnable() { @Override public void run() { try { //开启服务端 //等待客户端接入 while (true) { bluetoothServerSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(benjiname, Config.UUID); fuwuSocket = bluetoothServerSocket.accept(); if (fuwuSocket.isConnected()) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(BlueToothFindOthersAty.this, "接收挑战请求,建立连接成功!", Toast.LENGTH_SHORT); //执行socket方法 BlueToothGameAty blueToothGameAty = new BlueToothGameAty(); blueToothGameAty.blueToothGameAty.manageConnectedSocket(fuwuSocket, false); // blueToothGameAty.blueToothGameAty.chushihua(blueToothGameAty); } }); //跳转到蓝牙游戏activity Intent i = new Intent(BlueToothFindOthersAty.this,BlueToothGameAty.class); startActivity(i); //初始化线程来传输数据 // manageConnectedSocket(fuwuSocket); //得到连接之后关闭ServerSocket // bluetoothServerSocket.close(); //打断线程 // Thread.interrupted(); } } } catch (IOException e) { e.printStackTrace(); Log.d("whalea", "没读到的原因!:" + e.getMessage()); } } }).start();客户端主动连接模块:
/** * 建立连接的方法 * * @param position 位置 * @param isfaqiren */ private void buildConnect(int position, boolean isfaqiren) { //自己主动去连接 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceNameAndDresss.get(position).getDeviceAddress()); Boolean result = false; try { //先进行配对 //如果没有配对 Log.d("whalea", "开始配对"); if (device.getBondState() == BluetoothDevice.BOND_NONE) { Method createBondMethod = null; createBondMethod = BluetoothDevice.class .getMethod("createBond"); Log.d("whalea", "开始配对"); result = (Boolean) createBondMethod.invoke(device); } //如果已经配对好了 else if (device.getBondState() == BluetoothDevice.BOND_BONDED) { //获得客户端Socket kehuduanSocket = device.createRfcommSocketToServiceRecord(Config.UUID); final AlertDialog aDialog = new AlertDialog.Builder(BlueToothFindOthersAty.this). setTitle("发起对战"). setMessage("确认挑战玩家:" + deviceNameAndDresss.get(position).getDeviceName() + "吗?") .setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { new Thread(new Runnable() { @Override public void run() { //先停止扫描,以防止之后的连接被阻塞 bluetoothAdapter.cancelDiscovery(); try { //开始连接,发送连接请求 kehuduanSocket.connect(); if (!bluetoothAdapter.isEnabled()) { bluetoothAdapter.enable(); } if (kehuduanSocket.isConnected()) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(BlueToothFindOthersAty.this, "连接成功!!", Toast.LENGTH_SHORT).show(); //执行socket方法 BlueToothGameAty blueToothGameAty = new BlueToothGameAty(); blueToothGameAty.blueToothGameAty.manageConnectedSocket(kehuduanSocket, true); // blueToothGameAty.blueToothGameAty.chushihua(blueToothGameAty); } }); //跳转到蓝牙游戏activity Intent i = new Intent(BlueToothFindOthersAty.this,BlueToothGameAty.class); startActivity(i); } } catch (final IOException e) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(BlueToothFindOthersAty.this, "连接失败!!" + e.getMessage(), Toast.LENGTH_SHORT).show(); } }); /* try { kehuduanSocket.close(); } catch (IOException e1) { } return;*/ } // manageConnectedSocket(kehuduanSocket); //之后关闭socket,清除内部资源 /* try { kehuduanSocket.close(); } catch (IOException e) { e.printStackTrace(); }*/ } }).start(); } }) .setPositiveButton("取消", null).show(); } } catch (Exception e) { e.printStackTrace(); } }