用Dart寫的黑白棋遊戲

2022-01-28 19:38:39 字數 4321 閱讀 7623

2023年11月,dart語言1.0穩定版sdk發布,普天同慶。從此,網頁程式設計不再糾結了。

在我看來,dart語法簡直就是c#的公升級版,太像了。之所以喜歡ruby的乙個重要理由是支援mixin功能,而dart也引入了mixin特性。

最棒的是google提供了整合開發環境——dart editor和dartium,有非常強大的編輯和除錯功能,既能編寫網頁程式,也能編寫伺服器端程式。

第乙個程式寫什麼呢?做了乙個黑白棋遊戲。

直接上**:

doctype html

>

<

html

>

<

head

>

<

meta

charset

="utf-8"

>

<

title

>reversi

title

>

<

link

rel="stylesheet"

href

="reversi.css"

>

head

>

<

body

>

<

h1>黑白棋

h1>

<

p id

="score"

><

span

class

="black piece"

>

span

>

<

span

id="blackscore"

>2

span

>

<

span

class

="spacer"

>

span

><

span

class

="white piece"

>

span

>

<

span

id="whitescore"

>2

span

>

p>

<

table

id="board"

>

table

>

<

p id

="message"

>

p>

<

script

type

src="reversi_ui.dart"

>

script

>

<

script

src="packages/browser/dart.js"

>

script

>

body

>

html

>

棋盤介面的生成和使用者事件處理:

import '

dart:html';

import

'reversi_game.dart';

var chess = new

chessboard();

var cells = new map();

var blackscore = queryselector("

#blackscore");

var whitescore = queryselector("

#whitescore");

var message = queryselector("

#message");

void

main()

} updatecells();

}void

updatecells()

});blackscore.text =chess.blackscore.tostring();

whitescore.text =chess.whitescore.tostring();

message.text =chess.message;

}void placepiece(mouseevent event

) else

if(boardcell.isempty)

}

遊戲的邏輯部分:

class

boardcell

}class

cellpos

class

chessboard

cells.add(row);

}cells[

3][3].piece =white;

cells[

3][4].piece =black;

cells[

4][3].piece =black;

cells[

4][4].piece =white;

tip = "

遊戲開始";

} switchturn()

string getreverse(string piece)

else

if(piece ==white)

else

} bool

placepiece(boardcell cell)

else無棋"

; }

else

}return

true

; }

else

}return

false

; }

static listw(int row, int

col)

return

adjacentcells;

} static liste(int row, int

col)

return

adjacentcells;

} static listn(int row, int

col)

return

adjacentcells;

} static lists(int row, int

col)

return

adjacentcells;

} static listne(int row, int

col)

return

adjacentcells;

} static listse(int row, int

col)

return

adjacentcells;

} static listsw(int row, int

col)

return

adjacentcells;

} static listnw(int row, int

col)

return

adjacentcells;

} var directions =[n, ne, e, se, s, sw, w, nw];

list

findreverible(boardcell cell, string piece)

else

break

; }}}

return

allopponents;

} bool

reverseopponents(boardcell cell, string piece));

return

true

; }

else

} bool

canplacepiece(string piece)}}

return

false

; }

calculatescore()

else

if(piece ==black)}}

} var players = ;

string

getmessage

else

if(whitescore

else

}if(tip == null

)走棋"

; }

else走棋"

; }

}}

view code

點此開啟

以上只是乙個簡單的介面和走棋邏輯,接下來要做的是加入人工智慧,可以人機對戰;使用websocket通訊,實現聯網對戰。

這部分的**就不貼上來了。

點此開啟試玩

聯網對弈功能需要執行遊戲伺服器程式,目前只能在區域網使用。

黑白棋遊戲

黑白棋遊戲 time limit 10000ms memory limit 65536k total submit 9 accepted 5 case time limit 1000ms description 問題描述 黑白棋遊戲的棋盤由4 4方格陣列構成。棋盤的每一方格中放有1枚棋子,共有8枚白...

黑白棋遊戲

用c 封裝了一下,只完成了乙個雛形,有很多功能沒有新增上,但 的行數已經縮短了很多了。include include include includeusing namespace std class chess int counter 計數器,計算棋子個數 const friend void dra...

黑白棋遊戲

include include using namespace std char e 30 30 int a 30 30 int c 30 int x 30 int y 30 intf int n,int sum int pos sum 2 n int cnt 0 int flag 0 int w ...