求用dw制作计算器代码五子棋的代码

一个五子棋的代码 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 9时,
闲来无聊,花了2小时做了个html版的五子棋还有个bug,当屏幕放缩的时候:放子的位置和实际放子的位置不同解决方案,当屏幕放缩时更改单元格的值(源码中全局变量&var&w=25;)
代码片段(4)
index.html&~&3KB&&&&
&!DOCTYPE html&
&meta charset="utf-8"&
&meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=3, minimum-scale=1.0, maximum-scale=3.0"&
&title&在线五子棋&/title&
&canvas id="myCanvas" width="351" height="351" onclick="downChessman(event)"&&/canvas&
&script type="text/javascript"&
var w=25;//方格宽度
var cxt=null,myCanvas=//
var arrays=new Array();
window.onload=function(){
myCanvas=document.getElementById("myCanvas");
cxt=myCanvas.getContext("2d");
cxt.translate(0.5,0.5);
function init(){
cxt.strokeStyle="#666";
for(var i = 0;i&16;i++){
cxt.moveTo(w*i,0);
cxt.lineTo(w*i,14*w);
cxt.stroke();
cxt.restore();
for(var i = 0;i&16;i++){
cxt.moveTo(0,w*i);
cxt.lineTo(14*w,w*i);
cxt.stroke();
cxt.restore();
initDrawPoint();
initArray();
function initDrawPoint(){
cxt.fillStyle="#000";
for(var i=3;i&15;){
for(var j=3;j&15;){
cxt.beginPath();
cxt.arc(w*i,w*j,2,0,Math.PI*2,true);
cxt.fill();
cxt.beginPath();
cxt.arc(w*7,w*7,3,0,Math.PI*2,true);
cxt.fill();
function initArray(){
for(var i=0;i&16;i++){
arrays[i]=new Array();
for(var j=0;j&16;j++){
arrays[i][j]=0;
function drawChessman(i,j,type){
var img = new Image();
img.onload = function(){
cxt.drawImage(img, w*i-10,w*j-10);
//检查是否已经赢了
checkWin();
if(type){//是黑子
img.src="b.png";
img.src="w.png";
function downChessman(e){
var ox = myCanvas.offsetLeft,oy=myCanvas.offsetT
var x = Math.round((e.clientX-ox) / w);
var y = Math.round((e.clientY-oy) / w);
//console.log("cx="+(e.clientX-ox)+",cy="+(e.clientY-oy));
//console.log("x="+x+",y="+y);
var val = arrays[x][y];
if(val==0){//没有棋子
if(type){//黑子
arrays[x][y]=1;
arrays[x][y]=2;//白子
drawChessman(x,y,type);
function checkWin(){
var b0=b1=w0=w1=0;
for(var x=0;x&16;x++){
for(var y=0;y&16;y++){
var val = arrays[x][y];
if(val&0){//有棋子
if(y&16-5&&val==arrays[x][y+1]&&val==arrays[x][y+2]&&val==arrays[x][y+3]&&val==arrays[x][y+4]){//y轴连续5颗相同棋子
alert(val==1?"黑子获胜":"白子获胜");
if(x&16-5&&y&16-5&&val==arrays[x+1][y+1]&&val==arrays[x+2][y+2]&&val==arrays[x+3][y+3]&&val==arrays[x+4][y+4]){//西北-&东南方向连续5颗相同棋子
alert(val==1?"黑子获胜":"白子获胜");
if(x&16-5&&val==arrays[x+1][y]&&val==arrays[x+2][y]&&val==arrays[x+3][y]&&val==arrays[x+4][y]){//x轴连续5颗相同棋子
alert(val==1?"黑子获胜":"白子获胜");
if(x-4&=0&&val==arrays[x-1][y+1]&&val==arrays[x-2][y+2]&&val==arrays[x-3][y+3]&&val==arrays[x-4][y+4]){
alert(val==1?"黑子获胜":"白子获胜");
2.&[图片] b.png&&&&
3.&[图片] w.png&&&&
4.&[图片] QQ截图48.png&&&&
开源中国-程序员在线工具:
相关的代码(524)
[JavaScript]
[JavaScript]
[JavaScript]
[JavaScript]
[JavaScript]
[JavaScript]
[JavaScript]
[JavaScript]
[JavaScript]
[JavaScript]
开源从代码分享开始
黄途文的其它代码程序人生(17)
package&pkg2;
import&java.awt.*;
import&javax.swing.*;
import&java.awt.event.*;
public&class&Qizi&{
&&private&JFrame&frame;
private&MyPanel&panel1;
private&JPanel&panel2;
private&int&gamemodel=0;
public&static&void&main(String[]&args)&{
&Qizi&wo=new&Qizi();
&void&go(){
&frame=new&JFrame("五子棋--李亚勇");
panel1=new&MyPanel();
panel2=new&JPanel();
frame.setLayout(null);
frame.setSize(615,720);
panel2.setLayout(null);
panel2.setSize(580,580);
panel2.setBackground(Color.orange);
panel2.add(panel1);
panel1.setLocation(10,10);
frame.add(panel2);
panel1.setSize(570,570);
panel1.setBackground(Color.ORANGE);
panel2.setLocation(10,70);
JMenuBar&menubar=new&JMenuBar();
frame.setJMenuBar(menubar);
JMenu&menu1=new&JMenu("Choice");
JMenuItem&m1=new&JMenuItem("人人对战");
m1.addActionListener(new&M1());
JMenu&m2=new&JMenu("人机对战");
JRadioButtonMenuItem&m21=new&JRadioButtonMenuItem("简单",true);
m21.addActionListener(new&M21());
JRadioButtonMenuItem&m22=new&JRadioButtonMenuItem("中等");
m22.addActionListener(new&M22());
JRadioButtonMenuItem&m23=new&JRadioButtonMenuItem("困难");
m23.addActionListener(new&M23());
m2.add(m21);
m2.add(m22);
m2.add(m23);
ButtonGroup&group=new&ButtonGroup();
group.add(m21);
group.add(m22);
group.add(m23);
JMenuItem&m3=new&JMenuItem("退出");
m3.addActionListener(new&M3());
menu1.add(m1);
menu1.add(m2);
menu1.addSeparator();
menu1.add(m3);
JMenu&menu2=new&JMenu("Set");
JMenu&menu3=new&JMenu("About");
JMenuItem&m31=new&JMenuItem("关于我");
menu3.add(m31);
m31.addActionListener(new&Menu3());
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
frame.setResizable(false);
frame.setVisible(true);
class&MyPanel&extends&JPanel&implements&MouseListener,MouseMotionListener&{
private&int&x,y,pk11=0,pk22=0;
private&int&[][]chessmap=new&int[15][15];
private&int&qsjh=1;
private&int&bj[]={0,40,80,120,160,200,240,280,320,360,400,440,480,520,560};
void&chushihua(){
for(i=0;i&15;i++)
for(j=0;j&15;j++)
chessmap[i][j]=0;
MyPanel(){
addMouseListener(this);
addMouseMotionListener(this);
protected&void&paintComponent(Graphics&e){
super.paintComponent(e);
for(int&js=0;js&=560;js+=40)
&&&&e.drawLine(0,js,560,js);
&&&&&&&&&&&&for(int&js=0;js&=560;js+=40)
&&&&e.drawLine(js,0,js,560);
e.setColor(Color.black);
e.fillOval(115,115,10,10);
e.fillOval(435,115,10,10);
e.fillOval(115,435,10,10);
e.fillOval(435,435,10,10);
e.fillOval(275,275,10,10);
for(i=0;i&15;i++)
for(j=0;j&15;j++)
&&&&&&&&draw(e,i,j);
&&&&&&&&public&void&draw(Graphics&g,&int&i,&int&j){
if(chessmap[i][j]==1){
&&&&g.setColor(Color.black);
&&&&g.fillOval(40*i-20,40*j-20,40,40);
&&&&//pdsy();
if(chessmap[i][j]==2){
g.setColor(Color.white);
g.fillOval(40*i-20,40*j-20,40,40);
public&void&mousePressed(MouseEvent&e)&{
if(pk11&5&&pk22&5){
if(gamemodel==0){}
else&if(gamemodel==1){
&&&x=pdbj(e.getX());
&&&y=pdbj(e.getY());
&&&if(chessmap[x][y]!=0){}
&&if(qsjh==1){
&&&&&&&&&chessmap[x][y]=1;
&&&&&&&&&qsjh=2;
&&&&&&else&if(qsjh==2){
&&&&&chessmap[x][y]=2;
&&&&&qsjh=1;
&&&repaint();
&&&pdsy();
&&&x=pdbj(e.getX());
&&&y=pdbj(e.getY());
&&&if(chessmap[x][y]!=0){}
&&chessmap[x][y]=1;
&&repaint();
&&if(pk11&5&&pk22&5){
&&&&&jsjcq();
&&&&&repaint();
public&void&mouseReleased(MouseEvent&e)&{}
public&void&mouseEntered(MouseEvent&e)&{}
public&void&mouseExited(MouseEvent&e)&{}
public&void&mouseClicked(MouseEvent&e)&{}
public&void&mouseDragged(MouseEvent&e)&{}
public&void&mouseMoved(MouseEvent&e)&{}
int&pdbj(int&xx){
for(i0=0;i0&15;i0++)
if(((xx-bj[i0])&20)&&((xx-bj[i0])&-20))
xx=bj[i0];
return(xx/40);
void&jsjcq(){
int&max_black,max_white,max_temp,max=0;
int&x=0,y=0;
for(int&i=0;i&15;i++){
&&&&for(int&j=0;j&15;j++){
&&&&&&&&if(chessmap[i][j]==0){
&&&&&&&&&&&max_white=checkMax(i,j,2);
&&&&&&&&&&&max_black=checkMax(i,j,1);
&&&&&&&&&&&max_temp=Math.max(max_white,max_black);
&&&&&&&&&&&if(max_temp&max){
&&&&&&&&&&&&&&max=max_
&&&&&&&&&&&&&&x=i;
&&&&&&&&&&&&&&y=j;
&&&&&&&&&&&}
&&&&&&&&&&&&chessmap[x][y]=2;
int&checkMax(int&x,&int&y,int&black_or_white){
&&&&int&num=0,max_num,max_temp=0;
int&x_temp=x,y_temp=y;
int&x_temp1=x_temp,y_temp1=y_
&&&&//judge&right
for(int&i=1;i&5;i++){
x_temp1+=1;
if(x_temp1&14)
if(chessmap[x_temp1][y_temp1]==black_or_white)
&&&&//judge&left
&&&&x_temp1=x_
&&&&for(int&i=1;i&5;i++){
&&&&x_temp1-=1;
&&&&if(x_temp1&0)
&&&&break;
&&&&if(chessmap[x_temp1][y_temp1]==black_or_white)
&&&&&&&num++;
&&&&break;
&&&max_temp=
&&&//judge&up
&&&x_temp1=x_
&&&y_temp1=y_
&&&for(int&i=1;i&5;i++){
&&&&&&&y_temp1-=1;
&&&&&&&if(y_temp1&0)
&&&&&&&break;
&&&&&&&if(chessmap[x_temp1][y_temp1]==black_or_white)
&&&&&&&&&&num++;
&&&&&&&else
&&&&&&&break;
&&&//judge&down
&&&y_temp1=y_
&&&for(int&i=1;i&5;i++){
&&&&&&&y_temp1+=1;
&&&&&&&if(y_temp1&14)
&&&&&&&break;
&&&&&&&if(chessmap[x_temp1][y_temp1]==black_or_white)
&&&&&&&&&&num++;
&&&&&&&else
&&&&&&&break;
&&&if(num&max_temp&&num&5)
&&&max_temp=
&&&//judge&left_up
&&&x_temp1=x_
&&&y_temp1=y_
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:32666次
排名:千里之外
原创:128篇
转载:17篇
(1)(4)(1)(4)(8)(5)(6)(1)(3)(1)(1)(4)(3)(104)(1)(2)vb6.0 的五子棋源代码_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
vb6.0 的五子棋源代码
上传于||文档简介
&&v​b. ​的​五​子​棋​源​代​码​
​
​
​
​仅​供​v​b​学​习​者​交​流​之​用
你可能喜欢很好的一个五子棋源代码_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
很好的一个五子棋源代码
上传于||文档简介
&&五​子​棋​代​码
你可能喜欢

我要回帖

更多关于 dwcs6如何制作代码 的文章

 

随机推荐