象棋遊戲程式設計 畫棋盤

2022-02-26 07:00:35 字數 3653 閱讀 9164

最近一段時間開始玩象棋,雖然棋藝不精,確也樂在其中。作為象棋初學者,很多高手對我都是避而遠之,在我的威逼利誘之下,才跟我對戰一二。後來有位朋友推薦我玩單機版的象棋遊戲,在佩服其演算法精湛的同時,也出現了不少問題。比如說,和電腦對戰的時候,我明明士可以退回去吃他的車,可我的士卻不聽使喚。類似情況還有很多,這使我萌發了自己寫一款象棋遊戲的想法。

主功能介面已劃分完成,昨天寫了一段**畫棋盤(也是象棋裡面所說的九宮格)。畫棋盤雖然是個不起眼的小功能,但是棋盤上論輸贏,畫棋盤也是非常重要的。

在panel1中畫棋盤

public

void

panel1_paint(

object

sender, system.windows.forms.painteventargs e)

else

intindex=0

; //

記錄點的索引            

width

=panel1.width;

height

=panel1.height;

width

=width/9

;  height

=height/10

;  //

確定每個方格的寬和高

width-=4

; height-=3

;            

pen pen1

=new

pen(color.darkblue,

3);    

pen pen2

=new

pen(color.black,2);

intstartx

=startleft,starty

=starttop;

for(

inti=0

;i<2;i

++)point[index]

=new

point(point[index-1

].x+

width,point[index-1

].y);

index++;

starty

+=height;  

//往下移動動座標,畫下一行方格

startx

=startleft;

}                

//記錄上面往下的九個點

for(

intii

=index-9

,j=0;j

<

9;ii

++,j++)

intpaox,paoy;  

//宣告畫炮點的座標

intx,y,temp;   

//畫將/帥的輔助變數

if(i==0

)else

//畫橫向的兩個炮的圖案

drawallsitepoint(sender,e,paox,paoy,width);        

paox

=startleft+7

*width;

drawallsitepoint(sender,e,paox,paoy,width);    

//畫將/帥的皇宮        

float

dashvalues=;

pen2.dashpattern

=dashvalues;

g.drawline(pen2,x,starttop

+temp,x+2

*width,y);x=

startleft+5

*width;

g.drawline(pen2,x,starttop

+temp,x-2

*width,y);

//畫中間的楚河漢界                

font font

=new

font(

"華文行楷

", 42f, ((system.drawing.fontstyle)((system.drawing.fontstyle.bold 

|system.drawing.fontstyle.italic))), system.drawing.graphicsunit.point, ((system.byte)(

134)));                                

brush brush

=new

solidbrush(color.purple);

pointf temppoint

=new

pointf(startleft,starttop+4

*height+10

);string

tempstr;if(

this

.displaysize

==15

)else

g.drawstring(tempstr,font,brush,temppoint);

startx

=startleft;

starty

=starttop+5

*height

+height/2

;    

//移動座標,畫下方旗盤            

}            

for(

inti=29

;i<=

33;i+=2

)drawallsitepoint(sender,e,

this

.point[i].x,

this

.point[i].y,width);

for(

inti=56

;i<=

60;i+=2

)drawallsitepoint(sender,e,

this

.point[i].x,

this

.point[i].y,width);

this

.drawlefthalfsitepoint(sender,e,

this

.point[

27].x,

this

.point[

27].y,width);

this

.drawrighthalfsitepoint(sender,e,

this

.point[

35].x,

this

.point[

35].y,width);

this

.drawlefthalfsitepoint(sender,e,

this

.point[

54].x,

this

.point[

54].y,width);

this

.drawrighthalfsitepoint(sender,e,

this

.point[

62].x,

this

.point[

62].y,width);

pen1.dispose();

g.dispose();    

myinitializecomponent();

}下面是棋盤執行效果:

Qt象棋遊戲 02 繪畫象棋棋盤

進行 編寫之前,在chessarea.h 增加相關成員和方法定義,同時加入qt需要使用到的基本標頭檔案。主要用到qt繪畫類 qpen,qbrush,qpoint,qpaintevent等,重寫paintevent,達到繪製棋盤線功能。ifndef chessarea h define chessar...

棋盤類遊戲程式設計

簡單的l型圖案 三個單元格 在棋盤中心位置的形態為 const int covertype 4 3 2 當然根據座標系設定方位的不同,最終圖案的三維表示也不盡相同,只需統一即可,以及最好將座標系的中心置於棋盤的中心位置 這樣方位之間便會形成正負對稱的情況 5 5 棋盤上的英文本母格仔,規則是連線上下...

列印西洋棋棋盤

bin bash 列印西洋棋棋盤 設定兩個變數,i 和 j,乙個代表行,乙個代表列,西洋棋為 88 棋盤 i 1 是代表準備列印第一行棋盤,第 1 行棋盤有灰色和藍色間隔輸出,總共為 8 列 i 1,j 1 代表第 1 行的第 1 列 i 2,j 3 代表第 2 行的第 3 列 棋盤的規律是 i j...