Compare commits
252 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2fd04238f5 | ||
|
|
c7bad7665f | ||
|
|
b2d1d3f809 | ||
|
|
fe8f8621d7 | ||
|
|
767e7d14fd | ||
|
|
074f181791 | ||
|
|
542dd39aa6 | ||
|
|
6db4ae6d07 | ||
|
|
171a7b8020 | ||
|
|
205e8811d5 | ||
|
|
2394da84ce | ||
|
|
e7c7743682 | ||
|
|
2375f28ee3 | ||
|
|
036f57daa0 | ||
| a0bca32733 | |||
|
|
5b6318442e | ||
|
|
24ddc74573 | ||
| bdbe93932d | |||
| 75c39ccbe4 | |||
| df02c2eee1 | |||
| 5763716848 | |||
| 2112109470 | |||
|
|
933bb46f17 | ||
| 5deebb0621 | |||
| 60911f93f6 | |||
| 7820555fa8 | |||
| b5c18419d8 | |||
|
|
32ab155ff5 | ||
| 1f3fff3501 | |||
|
|
b8113b8cc4 | ||
|
|
3795087b3c | ||
|
|
7fdd7fd65c | ||
|
|
90a59305ac | ||
| 5ccca3ab8e | |||
| f2adc14ce2 | |||
| 7ed9ae3799 | |||
| 622bef963f | |||
|
|
499b0fd152 | ||
|
|
eba8076ea9 | ||
|
|
f05b1c80da | ||
|
|
068f5248cd | ||
|
|
a63954fbfe | ||
|
|
2b7faf80ab | ||
|
|
890fab38cb | ||
|
|
ebcc447092 | ||
|
|
a4982c0fd3 | ||
|
|
c414485891 | ||
|
|
f29298731b | ||
|
|
e1d51fe291 | ||
|
|
f841fba080 | ||
|
|
821746d7cf | ||
|
|
5d108602ea | ||
|
|
b137e6ceab | ||
|
|
d649afdcfd | ||
|
|
5e65bcf5ef | ||
|
|
82a6bd8449 | ||
|
|
23c890f329 | ||
|
|
b91777a64f | ||
|
|
775083fde7 | ||
|
|
84121fbeba | ||
|
|
f3d316d3e3 | ||
|
|
fad1a6d51b | ||
|
|
9b43c9ce9a | ||
|
|
dba7aca2cf | ||
|
|
47a22dbbaa | ||
|
|
1bbe33f3aa | ||
|
|
8c0fa09f1e | ||
|
|
d8eae9177b | ||
|
|
71ea2a05d0 | ||
|
|
33ca535547 | ||
| 6fc1b6c3fd | |||
| a985182c99 | |||
| 3b78a8e925 | |||
| ab369f179c | |||
| be6a86bebf | |||
|
|
08ba7ee436 | ||
|
|
5b3e84dab0 | ||
|
|
21d26902f0 | ||
|
|
e9f7663488 | ||
|
|
c2abc08206 | ||
|
|
9fa298ed51 | ||
|
|
a51d5e2652 | ||
|
|
0dd9a3d047 | ||
|
|
e896aba4ec | ||
|
|
9fb3d15bed | ||
|
|
35cb1eeb73 | ||
|
|
8165749845 | ||
|
|
06cb2b88c2 | ||
|
|
2d646f0ad0 | ||
|
|
40e2c50209 | ||
|
|
2e82a52f27 | ||
|
|
372d86b451 | ||
|
|
d66787f51d | ||
|
|
a4c63f19e5 | ||
|
|
a2f261778b | ||
|
|
3f7cc2df4d | ||
|
|
48272d6d70 | ||
|
|
d172afc50c | ||
| 5336aadf97 | |||
|
|
ee61528891 | ||
|
|
0ace485bb6 | ||
|
|
1c1b9a96d9 | ||
|
|
e99b2defa3 | ||
|
|
f39c113ef9 | ||
|
|
f417a7e47c | ||
|
|
547b0e51cb | ||
|
|
5340744abe | ||
|
|
f64ebfa47f | ||
|
|
4e7ac2a195 | ||
|
|
dc176c103b | ||
|
|
85a7fa37ef | ||
|
|
a60658763d | ||
|
|
b76a009e4e | ||
|
|
db333a693f | ||
|
|
fd0d26486b | ||
| 8d559f4b11 | |||
|
|
e1f4ae717e | ||
|
|
7b9c1edbab | ||
|
|
05294a7736 | ||
|
|
d8da808580 | ||
|
|
c420d8b3dd | ||
|
|
57af3aaae3 | ||
|
|
887b9abed8 | ||
|
|
a862d31c7d | ||
| 48f66aac75 | |||
|
|
431c65e075 | ||
|
|
7e64a7ca16 | ||
|
|
6e0efc76f3 | ||
|
|
1a9d7ad460 | ||
| 6dfe92f211 | |||
|
|
9209f1c4e0 | ||
| 4bb485e833 | |||
|
|
3ebfc61ac7 | ||
|
|
d8d8b58ed7 | ||
|
|
a8970053f9 | ||
|
|
7cd77ab904 | ||
|
|
fa0a93172d | ||
|
|
bb74a486d8 | ||
|
|
10c176a0f5 | ||
|
|
d1932762f9 | ||
|
|
9181b6c4ca | ||
|
|
b703d8b9a1 | ||
|
|
acaa7d078a | ||
|
|
30448b5f3d | ||
|
|
ff38c5c475 | ||
|
|
42daeb9971 | ||
|
|
fc7ece9e94 | ||
|
|
c8b6bc35d5 | ||
|
|
5f2a4e1721 | ||
| c77e534ed8 | |||
|
|
5c42b6e52a | ||
|
|
f7364e7939 | ||
| 1c92918692 | |||
|
|
b4a1f0820f | ||
|
|
6d9d5c49ae | ||
|
|
1ca44b472b | ||
|
|
79aabeac8c | ||
|
|
5eb85dab89 | ||
|
|
291b7e71a2 | ||
|
|
2aaa2aca70 | ||
|
|
456d386551 | ||
| cca852d466 | |||
|
|
e663d61b9e | ||
| a098c8051a | |||
|
|
c3ed181b07 | ||
|
|
5b3f2488d4 | ||
| e599722e45 | |||
|
|
659413ca31 | ||
|
|
4de505bc21 | ||
|
|
7c58f0d508 | ||
|
|
c9246d1342 | ||
|
|
eab795b6df | ||
|
|
f8ab14a026 | ||
|
|
274897a070 | ||
|
|
8ecbeb9c41 | ||
|
|
a1482d11f2 | ||
|
|
f9a302c9a0 | ||
|
|
ad530e9155 | ||
|
|
182aa59ee1 | ||
|
|
296f1f6c3a | ||
|
|
f7355f8e74 | ||
|
|
cd58a7a321 | ||
|
|
2beb8ab303 | ||
|
|
5425ccc5cd | ||
|
|
1104c8e6c5 | ||
|
|
274ffcf5ec | ||
|
|
5854dbc20b | ||
|
|
f36a196b2f | ||
|
|
11f26809df | ||
|
|
c88fbe68e3 | ||
|
|
7f4c53ddb7 | ||
|
|
38b38845d6 | ||
|
|
66dd2877b2 | ||
|
|
3cd53c7d70 | ||
|
|
5b3bdc750f | ||
| 83a73ed34c | |||
|
|
7c27d47935 | ||
|
|
4ffec0f05d | ||
|
|
da3b1c8c00 | ||
|
|
a497f8554f | ||
| 94b4d727a6 | |||
| 39273908ac | |||
|
|
7be3c928b7 | ||
|
|
f8c14cc268 | ||
|
|
566d72c2d6 | ||
| ad26d22281 | |||
| 352b4e57d7 | |||
| 5696128ac9 | |||
| c2e29415c9 | |||
| ff68d9d2d9 | |||
| 281f496c83 | |||
| 532c5a42ea | |||
| 73624baf90 | |||
| d8861b66b2 | |||
| 4123a710cf | |||
| 4daa21e8bf | |||
|
|
cdd8b45da8 | ||
| 503cb23015 | |||
|
|
6006442f90 | ||
|
|
eebdfdbee2 | ||
|
|
a6aba8801e | ||
|
|
8d1300d7e2 | ||
|
|
d0e6ce81ce | ||
|
|
f3dea86ded | ||
|
|
092ed19104 | ||
|
|
f8894bbdff | ||
|
|
258a8a0da9 | ||
| 8ffbfdc63f | |||
|
|
dca6eac3ba | ||
| 8f9a48fa96 | |||
|
|
5ee797e1f8 | ||
|
|
eaf3bfa192 | ||
|
|
9c73ca6838 | ||
|
|
0f50f31b13 | ||
|
|
b6f0b6ee5e | ||
| 4ae9eea7e2 | |||
| 1f368551c1 | |||
|
|
4e9f222ddc | ||
|
|
061795a039 | ||
|
|
1af497f063 | ||
|
|
b6cdf5b778 | ||
|
|
4eb4bc1348 | ||
|
|
c2fe2e968a | ||
|
|
21f2890b92 | ||
|
|
5d748b07d2 | ||
|
|
b252f16b2d | ||
|
|
18f4060df0 | ||
|
|
033440fbac | ||
|
|
bfb2a6db23 | ||
|
|
70d10719e6 | ||
|
|
1788cce42d | ||
|
|
49f4ffc380 |
11
.github/workflows/dispatcher.yml
vendored
11
.github/workflows/dispatcher.yml
vendored
@@ -16,6 +16,9 @@ jobs:
|
|||||||
server: ${{ steps.check.outputs.server }}
|
server: ${{ steps.check.outputs.server }}
|
||||||
ui: ${{ steps.check.outputs.ui }}
|
ui: ${{ steps.check.outputs.ui }}
|
||||||
steps:
|
steps:
|
||||||
|
- name: checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Determine which tests to run
|
- name: Determine which tests to run
|
||||||
id: check
|
id: check
|
||||||
run: |
|
run: |
|
||||||
@@ -26,13 +29,13 @@ jobs:
|
|||||||
SERVER=false
|
SERVER=false
|
||||||
UI=false
|
UI=false
|
||||||
|
|
||||||
if [[ "$BRANCH" == *"Engine"* ]]; then
|
if [[ "$BRANCH" == *"Engine"* ]] ; then
|
||||||
ENGINE=true
|
ENGINE=true
|
||||||
fi
|
fi
|
||||||
if [[ "$BRANCH" == *"Server"* ]]; then
|
if [[ "$BRANCH" == *"Server"* ]] ; then
|
||||||
SERVER=true
|
SERVER=true
|
||||||
fi
|
fi
|
||||||
if [[ "$BRANCH" == *"UI"* ]]; then
|
if [[ "$BRANCH" == *"UI"* ]] ; then
|
||||||
UI=true
|
UI=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -74,7 +77,7 @@ jobs:
|
|||||||
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
needs: test-data-upload
|
needs: [engine, server, ui]
|
||||||
if: github.ref == 'refs/heads/master'
|
if: github.ref == 'refs/heads/master'
|
||||||
uses: ./.github/workflows/release.yml
|
uses: ./.github/workflows/release.yml
|
||||||
secrets: inherit
|
secrets: inherit
|
||||||
|
|||||||
3
.github/workflows/engine_test.yml
vendored
3
.github/workflows/engine_test.yml
vendored
@@ -1,7 +1,6 @@
|
|||||||
name: Engine Tests
|
name: Engine Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
|
||||||
@@ -10,8 +9,6 @@ jobs:
|
|||||||
runs-on: self-hosted
|
runs-on: self-hosted
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Run Engine tests
|
- name: Run Engine tests
|
||||||
run: |
|
run: |
|
||||||
bash .github/workflows/test.sh engine/
|
bash .github/workflows/test.sh engine/
|
||||||
|
|||||||
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@@ -1,7 +1,6 @@
|
|||||||
name: Release build
|
name: Release build
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
|
||||||
@@ -13,9 +12,6 @@ jobs:
|
|||||||
name: Build and release (master only)
|
name: Build and release (master only)
|
||||||
runs-on: self-hosted
|
runs-on: self-hosted
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Build Engine for Linux
|
- name: Build Engine for Linux
|
||||||
run: |
|
run: |
|
||||||
cd $(git rev-parse --show-toplevel)
|
cd $(git rev-parse --show-toplevel)
|
||||||
|
|||||||
3
.github/workflows/server_test.yml
vendored
3
.github/workflows/server_test.yml
vendored
@@ -1,7 +1,6 @@
|
|||||||
name: Server Tests
|
name: Server Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
|
||||||
@@ -10,8 +9,6 @@ jobs:
|
|||||||
runs-on: self-hosted
|
runs-on: self-hosted
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Run Server tests
|
- name: Run Server tests
|
||||||
run: |
|
run: |
|
||||||
bash .github/workflows/test.sh server/
|
bash .github/workflows/test.sh server/
|
||||||
|
|||||||
5
.github/workflows/test.sh
vendored
5
.github/workflows/test.sh
vendored
@@ -30,8 +30,13 @@ echo "$PROJECT_NAME" > "$LOG_FILE"
|
|||||||
awk '/^running [0-9]+ test[s]?$/,/^$/' full_test_output.log >> "$LOG_FILE"
|
awk '/^running [0-9]+ test[s]?$/,/^$/' full_test_output.log >> "$LOG_FILE"
|
||||||
|
|
||||||
# --- APPEND TO GLOBAL LOG (in repo root) ---
|
# --- APPEND TO GLOBAL LOG (in repo root) ---
|
||||||
|
if [[ $(git rev-parse --abbrev-ref HEAD) == "master" ]]; then
|
||||||
|
echo "master" >> $FINAL_LOG
|
||||||
|
fi
|
||||||
cat "$LOG_FILE" >> "$FINAL_LOG"
|
cat "$LOG_FILE" >> "$FINAL_LOG"
|
||||||
|
|
||||||
# --- SUMMARY ---
|
# --- SUMMARY ---
|
||||||
echo ">>> Test output extracted to $PROJECT_PATH/$LOG_FILE"
|
echo ">>> Test output extracted to $PROJECT_PATH/$LOG_FILE"
|
||||||
echo ">>> Appended to $FINAL_LOG"
|
echo ">>> Appended to $FINAL_LOG"
|
||||||
|
|
||||||
|
cat $(git rev-parse --show-toplevel)/test_data.log
|
||||||
|
|||||||
3
.github/workflows/ui_test.yml
vendored
3
.github/workflows/ui_test.yml
vendored
@@ -1,7 +1,6 @@
|
|||||||
name: UI Tests
|
name: UI Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
|
||||||
@@ -10,8 +9,6 @@ jobs:
|
|||||||
runs-on: self-hosted
|
runs-on: self-hosted
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Run UI tests
|
- name: Run UI tests
|
||||||
run: |
|
run: |
|
||||||
bash .github/workflows/test.sh ui/
|
bash .github/workflows/test.sh ui/
|
||||||
|
|||||||
71
.github/workflows/upload_data.yml
vendored
71
.github/workflows/upload_data.yml
vendored
@@ -20,33 +20,78 @@ jobs:
|
|||||||
echo "$GOOGLE_SERVICE_ACCOUNT_JSON" > service_account.json
|
echo "$GOOGLE_SERVICE_ACCOUNT_JSON" > service_account.json
|
||||||
|
|
||||||
python <<'PYCODE'
|
python <<'PYCODE'
|
||||||
import gspread, json, time, subprocess
|
import gspread, json, subprocess, time
|
||||||
|
|
||||||
# credentials
|
|
||||||
creds = json.load(open("service_account.json"))
|
creds = json.load(open("service_account.json"))
|
||||||
gc = gspread.service_account_from_dict(creds)
|
gc = gspread.service_account_from_dict(creds)
|
||||||
sh = gc.open_by_key("${{ secrets.SPREADSHEET_ID }}")
|
sh = gc.open_by_key("${{ secrets.SPREADSHEET_ID }}")
|
||||||
v = subprocess.run(['git','rev-parse','--show-toplevel'], capture_output=True).stdout.decode().strip()
|
v = subprocess.run(['git','rev-parse','--show-toplevel'], capture_output=True).stdout.decode().strip()
|
||||||
print(f"{v}/test_data.log")
|
print(f"{v}/test_data.log")
|
||||||
|
|
||||||
|
|
||||||
|
def writeRowsToSpreadsheet(data_list, worksheet):
|
||||||
|
existing_rows = len(worksheet.get_all_values())
|
||||||
|
start_row = existing_rows + 3
|
||||||
|
rows_to_append = [row.split() for row in data_list]
|
||||||
|
print("rows to append")
|
||||||
|
print(f"{rows_to_append}")
|
||||||
|
|
||||||
|
for i, row in enumerate(rows_to_append):
|
||||||
|
worksheet.insert_row(row, start_row + i)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
with open(f"{v}/test_data.log", "r") as f:
|
with open(f"{v}/test_data.log", "r") as f:
|
||||||
lines = [line.strip() for line in f if line.strip()]
|
lines = [line.strip() for line in f if line.strip()]
|
||||||
|
|
||||||
|
isMaster = False
|
||||||
project = lines[0].lower()
|
project = lines[0].lower()
|
||||||
worksheet = sh.worksheet(project)
|
if project == "master":
|
||||||
|
isMaster = True
|
||||||
|
|
||||||
|
engine_data = []
|
||||||
|
server_data = []
|
||||||
|
ui_data = []
|
||||||
|
master_data = []
|
||||||
|
|
||||||
# project name
|
for entry in lines:
|
||||||
data = lines[1:]
|
if not isMaster and entry == "engine":
|
||||||
|
project = "engine"
|
||||||
|
elif not isMaster and entry == "server":
|
||||||
|
project = "server"
|
||||||
|
elif not isMaster and entry == "ui":
|
||||||
|
project = "ui"
|
||||||
|
|
||||||
#blank rows
|
if project == "engine" and entry != "engine":
|
||||||
existing_rows = len(worksheet.get_all_values())
|
engine_data.append(entry)
|
||||||
start_row = existing_rows + 3
|
elif project == "server" and entry != "server":
|
||||||
|
server_data.append(entry)
|
||||||
|
elif project == "ui" and entry != "ui":
|
||||||
|
ui_data.append(entry)
|
||||||
|
elif project == "master" and entry != "master":
|
||||||
|
master_data.append(entry)
|
||||||
|
|
||||||
# Split data into columns (by spaces)
|
print("PRINTING FILTERED DATA\n\n")
|
||||||
rows_to_append = [row.split() for row in data]
|
print(f"engine\n{engine_data}")
|
||||||
|
print(f"server\n{server_data}")
|
||||||
|
print(f"ui\n{ui_data}")
|
||||||
|
print(f"master\n{master_data}")
|
||||||
|
print("\n\n\n")
|
||||||
|
|
||||||
for i, row in enumerate(rows_to_append):
|
if isMaster and len(master_data) != 0:
|
||||||
worksheet.insert_row(row, start_row + i)
|
print("uploading to master tab")
|
||||||
|
worksheet = sh.worksheet("master")
|
||||||
|
writeRowsToSpreadsheet(master_data, worksheet)
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
if len(engine_data) != 0:
|
||||||
|
print("uploading to engine tab")
|
||||||
|
writeRowsToSpreadsheet(engine_data, sh.worksheet("engine"))
|
||||||
|
if len(server_data) != 0:
|
||||||
|
print("uploading to server tab")
|
||||||
|
writeRowsToSpreadsheet(server_data, sh.worksheet("server"))
|
||||||
|
if len(ui_data) != 0:
|
||||||
|
print("uploading to ui tab")
|
||||||
|
writeRowsToSpreadsheet(ui_data, sh.worksheet("ui"))
|
||||||
|
|
||||||
print(f"Uploaded {len(rows_to_append)} rows to '{project}' tab.")
|
|
||||||
PYCODE
|
PYCODE
|
||||||
|
|||||||
BIN
Docs/Knightly rendszerterv v1.png
Normal file
BIN
Docs/Knightly rendszerterv v1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 288 KiB |
287
Docs/Knightly rendszerterv v1.xml
Normal file
287
Docs/Knightly rendszerterv v1.xml
Normal file
@@ -0,0 +1,287 @@
|
|||||||
|
<mxfile host="app.diagrams.net" agent="Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0" version="28.2.9">
|
||||||
|
<diagram name="Page-1" id="9d-IwT1pk2NoWoP7MTJv">
|
||||||
|
<mxGraphModel dx="1534" dy="751" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="9999" pageHeight="9999" math="0" shadow="0">
|
||||||
|
<root>
|
||||||
|
<mxCell id="0" />
|
||||||
|
<mxCell id="1" parent="0" />
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-10" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-1" target="IaA3IaIvg4qVMEhu7ya9-36" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-1" value="App megnyitása" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="120" y="50" width="140" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-10" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-6" target="IaA3IaIvg4qVMEhu7ya9-9" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="540" y="120" />
|
||||||
|
<mxPoint x="370" y="120" />
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-12" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-6" target="IaA3IaIvg4qVMEhu7ya9-11" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-13" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-6" target="IaA3IaIvg4qVMEhu7ya9-11" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-14" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-6" target="IaA3IaIvg4qVMEhu7ya9-11" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-15" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-6" target="IaA3IaIvg4qVMEhu7ya9-11" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-16" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-6" target="IaA3IaIvg4qVMEhu7ya9-18" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<mxPoint x="670" y="180" as="targetPoint" />
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-21" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-6" target="IaA3IaIvg4qVMEhu7ya9-20" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-6" value="<div>főmenü</div>" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="480" y="40" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-24" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-9" target="IaA3IaIvg4qVMEhu7ya9-23" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-9" value="Lokális játék hosztolása" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="310" y="210" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-32" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-11" target="IaA3IaIvg4qVMEhu7ya9-31" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-11" value="<div>Lokális játékba csatlakozni</div><div><br></div>" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="460" y="220" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-13" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-18" target="4rzJeeoM2DMZDjvMm2AJ-12" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-18" value="Online jatek<br>(sorba allas)" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="695" y="200" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-22" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0;exitDx=0;exitDy=0;entryX=1;entryY=0;entryDx=0;entryDy=0;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-20" target="IaA3IaIvg4qVMEhu7ya9-6" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-28" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-20" target="IaA3IaIvg4qVMEhu7ya9-27" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-3" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="IaA3IaIvg4qVMEhu7ya9-20" target="IaA3IaIvg4qVMEhu7ya9-27">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-5" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="IaA3IaIvg4qVMEhu7ya9-20" target="iJwoCWMJ6fIoSaqHo3aH-4">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-6" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="IaA3IaIvg4qVMEhu7ya9-20" target="iJwoCWMJ6fIoSaqHo3aH-4">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-8" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="IaA3IaIvg4qVMEhu7ya9-20" target="iJwoCWMJ6fIoSaqHo3aH-7">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="770" y="80" />
|
||||||
|
<mxPoint x="1000" y="80" />
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-10" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=1;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="IaA3IaIvg4qVMEhu7ya9-20" target="iJwoCWMJ6fIoSaqHo3aH-9">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-20" value="<div>Beallitasok</div>" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="650" y="40" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-5" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-23" target="4rzJeeoM2DMZDjvMm2AJ-4" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-23" value="<div>Szerver peldany elinditasa</div>" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="130" y="150" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-31" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-25" target="IaA3IaIvg4qVMEhu7ya9-29" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-16" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="IaA3IaIvg4qVMEhu7ya9-25" target="iJwoCWMJ6fIoSaqHo3aH-15">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-25" value="varakozas az ellenfelre" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="130" y="390" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-27" value="lokalis jatekhoz port megvaltoztatasa" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="830" y="10" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-33" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-29" target="4rzJeeoM2DMZDjvMm2AJ-32" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-29" value="<div>jatek elkezdése</div>" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="400" y="850" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-34" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-31" target="IaA3IaIvg4qVMEhu7ya9-33" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-31" value="<div>lokalis halozaton szerver ip:port megadasa</div>" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="460" y="360" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-11" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-33" target="IaA3IaIvg4qVMEhu7ya9-29" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-33" value="csatlakozás" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="290" y="360" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-38" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="IaA3IaIvg4qVMEhu7ya9-36" target="IaA3IaIvg4qVMEhu7ya9-6" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="IaA3IaIvg4qVMEhu7ya9-36" value="user struct letrehozasa a app-ban" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="340" y="40" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-9" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-4" target="4rzJeeoM2DMZDjvMm2AJ-7" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-4" value="hostolo kapcsolodik a szerverehez" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="130" y="230" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-8" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-7" target="IaA3IaIvg4qVMEhu7ya9-25" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-7" value="eszköz ip cím lekérése a csatlakozáshoz" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="130" y="310" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-21" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-12" target="4rzJeeoM2DMZDjvMm2AJ-20" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-12" value="<div>csatlakozás a központi szerverhez</div>(tudjuk az ip cimet)" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="690" y="300" width="130" height="70" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-24" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-16" target="4rzJeeoM2DMZDjvMm2AJ-23" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-27" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-16" target="4rzJeeoM2DMZDjvMm2AJ-26" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-16" value="sorban állás szabad játékra várva" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="695" y="480" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-22" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-20" target="4rzJeeoM2DMZDjvMm2AJ-16" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-20" value="a szerver eltárolja az uj jatekost mint aktiv felhasznalo a listaban" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="700" y="390" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-23" value="a szerveren bekerultunk a varolistaba" style="ellipse;whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="850" y="465" width="155" height="90" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-29" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-26" target="4rzJeeoM2DMZDjvMm2AJ-28" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-26" value="<div>megjelent meg egy jatekos a varolistan</div>" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="695" y="570" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-30" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-28" target="IaA3IaIvg4qVMEhu7ya9-29" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-28" value="ket jatekos ossze vonasa egy meccsbe. Kivesszuk oket a varolistabol es egy Match structba osszevonjuk oket" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="682.5" y="660" width="145" height="120" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-35" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-32" target="4rzJeeoM2DMZDjvMm2AJ-34" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-32" value="<div>fehér és fekete oldal eldöntése</div><div>(szerver által, érme dobással)</div>" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="390" y="950" width="140" height="80" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-37" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-34" target="4rzJeeoM2DMZDjvMm2AJ-36" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-34" value="játék ui megjelenítése" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="400" y="1060" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-39" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-36" target="4rzJeeoM2DMZDjvMm2AJ-38" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-36" value="lehetséges lépések lekérése a tábla állapot alapján a szerver / engine-től" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="390" y="1160" width="140" height="90" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-41" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-38" target="4rzJeeoM2DMZDjvMm2AJ-40" edge="1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-38" value="a ui lokalisan eltarolja ezt az adot a következő körig" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="400" y="1280" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-2" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-40" target="iJwoCWMJ6fIoSaqHo3aH-1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-19" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="4rzJeeoM2DMZDjvMm2AJ-40" target="iJwoCWMJ6fIoSaqHo3aH-18">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4rzJeeoM2DMZDjvMm2AJ-40" value="a jatekos lep a egy babual. Ekkor ellenorizzuk hogy szabad e volt ezt a léppést megtennie" style="whiteSpace=wrap;html=1;rounded=0;" parent="1" vertex="1">
|
||||||
|
<mxGeometry x="390" y="1370" width="140" height="90" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-12" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="iJwoCWMJ6fIoSaqHo3aH-1" target="iJwoCWMJ6fIoSaqHo3aH-11">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-14" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="iJwoCWMJ6fIoSaqHo3aH-1" target="iJwoCWMJ6fIoSaqHo3aH-13">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-1" value="ha szabad volt elküldjük a lépést a szervernek" style="whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="400" y="1490" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-4" value="felbontás megváltoztatása" style="whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="830" y="90" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-7" value="teljes képernyő" style="whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="1000" y="50" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-9" value="ai api kulcs megadása" style="whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="1000" y="140" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-21" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="iJwoCWMJ6fIoSaqHo3aH-11" target="iJwoCWMJ6fIoSaqHo3aH-20">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-11" value="<div>ellenfél ui frissítése</div>" style="whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="400" y="1580" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-13" value="<div>{</div><div>from:str,</div><div>to: str</div><div>}</div>" style="rhombus;whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="560" y="1455" width="140" height="130" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-15" value="Ip cím megjelenítése a könnyebb csatlakozás érdekében" style="ellipse;whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="20" y="490" width="120" height="150" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-18" value="a ui végig megy az adatokon, amit visszakapott a szervertől a kör elején" style="ellipse;whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="590" y="1290" width="170" height="110" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-23" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="iJwoCWMJ6fIoSaqHo3aH-20" target="iJwoCWMJ6fIoSaqHo3aH-22">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-25" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="iJwoCWMJ6fIoSaqHo3aH-20" target="iJwoCWMJ6fIoSaqHo3aH-24">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-27" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="1" source="iJwoCWMJ6fIoSaqHo3aH-20">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<mxPoint x="640" y="1860" as="targetPoint" />
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-20" value="ellenőrizzük, hogy nyert-e a játékos vagy döntetlen lett-e" style="whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="395" y="1670" width="130" height="70" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-22" value="a szerver megkapja a tábla állást és elküldi az enginnek ami ad egy választ" style="ellipse;whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="560" y="1647.5" width="200" height="115" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-26" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="iJwoCWMJ6fIoSaqHo3aH-24" target="4rzJeeoM2DMZDjvMm2AJ-36">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="360" y="1820" />
|
||||||
|
<mxPoint x="360" y="1205" />
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-24" value="kör átadása az ellenfélnek" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="400" y="1790" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-30" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="iJwoCWMJ6fIoSaqHo3aH-28" target="iJwoCWMJ6fIoSaqHo3aH-29">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-28" value="játék befejezése, nyertes megjelnítése,&nbsp;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="580" y="1860" width="120" height="60" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="iJwoCWMJ6fIoSaqHo3aH-29" value="a játékosokat vissza küldjük a menübe, a szerver oldalon töröljük a meccset a listából, és töröljük&nbsp; a játékosokat a meccs közben lévő listából" style="whiteSpace=wrap;html=1;rounded=0;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="560" y="1940" width="160" height="120" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
</root>
|
||||||
|
</mxGraphModel>
|
||||||
|
</diagram>
|
||||||
|
</mxfile>
|
||||||
187
Docs/kovspec.md
Normal file
187
Docs/kovspec.md
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
# Követelmény-specifikáció
|
||||||
|
|
||||||
|
## 1. Áttekintés
|
||||||
|
|
||||||
|
A jelen dokumentum célja, hogy bemutassa a CastlingCreations megrendelésére készülő Knightly nevű alkalmazás alapvető céljait, funkcionális és nem funkcionális követelményeinek áttekintését, valamint a fejlesztés kontextusát.
|
||||||
|
|
||||||
|
A Knightly egy modern, digitális sakkalkalmazás, amely kezdetben helyi hálózaton (LAN) keresztül teszi lehetővé két játékos számára, hogy valós időben mérkőzzenek meg egymással. A rendszer egy grafikus felhasználói felületen keresztül biztosítja a játék indítását, a szerver futtatását és a másik félhez történő csatlakozást.
|
||||||
|
|
||||||
|
A projekt hosszú távú célja, hogy a Knightly egy online platformmá fejlődjön, amely hasonló módon működik, mint a népszerű sakkportálok (pl. chess.com): a felhasználók fiókot hozhatnak létre, bejelentkezhetnek, és egy központi szerveren keresztül kereshetnek, illetve indíthatnak mérkőzéseket.
|
||||||
|
|
||||||
|
A fejlesztés első szakasza azonban a LAN-alapú verzió megvalósítására koncentrál, amely a sakkjátszma logikai alapjainak, a játékállapot kezelésének és a hálózati kommunikáció modelljének megvalósítását célozza. A későbbi online verzió ezekre az alapokra építkezve bővíthető tovább.
|
||||||
|
|
||||||
|
## 2. Vágyálom rendszer
|
||||||
|
A vágyálom rendszer célja egy teljes körű, modern online sakkplatform létrehozása, amely nemcsak a klasszikus sakkjáték digitális megvalósítását kínálja, hanem egy közösségi, versenyképes és kényelmes felhasználói élményt is biztosít. A hosszú távú cél, hogy a rendszer működése és felépítése a nagyobb nemzetközi sakkoldalakhoz (például a chess.com-hoz vagy a lichess.org-hoz) hasonló legyen, de saját, könnyen kezelhető és letisztult felülettel.
|
||||||
|
|
||||||
|
A felhasználók a rendszerben saját profilt hozhatnak létre, amellyel be tudnak jelentkezni, és részt vehetnek online mérkőzéseken más játékosok ellen. A rendszer automatikusan párosítaná őket ellenfelekkel, de lehetőséget adna arra is, hogy barátokat hívjanak meg privát meccsekre. A lejátszott mérkőzések mentésre kerülnének, így a játékosok bármikor visszanézhetnék vagy elemezhetnék azokat.
|
||||||
|
|
||||||
|
A játék mellett a felhasználók statisztikákat is láthatnának magukról, például nyerési arányt, aktuális értékszámot (rating), leggyakoribb megnyitásokat, illetve fejlődési tendenciát az idő során. A rendszer ezen felül egy egyszerű chat funkciót is tartalmazna, hogy a játékosok kommunikálhassanak egymással a játszmák közben vagy akár azokon kívül is.
|
||||||
|
|
||||||
|
A vágyálom rendszer alapját egy központi szerver képezné, amely kezeli a felhasználói fiókokat, a bejelentkezéseket, a matchmaking folyamatot, valamint a játékok futását és szinkronizálását. A szerver a kliensalkalmazásokkal valós idejű adatkapcsolatot tartana fenn, így a játék során minden lépés azonnal megjelenne a másik játékosnál is.
|
||||||
|
|
||||||
|
A platform célja a megbízható és folyamatos működés, akár nagyobb terhelés mellett is. A rendszer fejlesztése során kiemelt szempont lenne a biztonság (adatvédelem, csalás elleni védelem), a stabil hálózati kommunikáció, valamint a bővíthetőség – például ranglisták, versenyek vagy mobilalkalmazás későbbi integrálásának lehetősége.
|
||||||
|
|
||||||
|
Összességében a vágyálom rendszer egy minden szempontból teljes értékű, közösségorientált sakkalkalmazás lenne, amely a mostani, helyi hálózaton működő változatból fejlődne tovább egy interneten keresztül bárhonnan elérhető platformmá.
|
||||||
|
|
||||||
|
## 3. Igényelt funkciók
|
||||||
|
|
||||||
|
### Alapok
|
||||||
|
- Két játékos közti sakkjátszma lebonyolítása.
|
||||||
|
- Teljes szabályrendszer megvalósítása (érvényes lépések, sakk/sakkmatt/patt felismerése).
|
||||||
|
- Új játék indítása.
|
||||||
|
- Játékosok nevének megadása a játszma elején.
|
||||||
|
- Felhasználóbarát grafikus felület (UI) – látható tábla, figurák, órák, státuszjelzések.
|
||||||
|
- Játékoslépések vizuális kiemelése (pl. kijelölt mező, lehetséges lépések megjelenítése).
|
||||||
|
- A játék állapotának kijelzése (folyamatban, sakk, matt, döntetlen).
|
||||||
|
|
||||||
|
### LAN és hálózati funkciók
|
||||||
|
- „Szerver indítása” funkció – a játékos hostként indíthat egy helyi szervert.
|
||||||
|
- „Csatlakozás” funkció – másik játékos IP-cím alapján tud csatlakozni.
|
||||||
|
- Helyi hálózaton keresztüli valós idejű kommunikáció.
|
||||||
|
- LAN játék automatikus felfedezése (broadcast keresés).
|
||||||
|
- Játék mentése és visszatöltése hálózati módban.
|
||||||
|
|
||||||
|
### Online vágyálom funkciók
|
||||||
|
- Felhasználói regisztráció és bejelentkezés.
|
||||||
|
- Jelszóval védett fiókok, email- vagy OAuth-alapú hitelesítés (Google, GitHub stb.).
|
||||||
|
- Profiloldal megtekintése (név, avatar, statisztikák, értékszám).
|
||||||
|
- Automatikus matchmaking rendszer.
|
||||||
|
- Kézi játékindítás – meghívó küldése barátnak.
|
||||||
|
- Játszmák mentése és visszajátszása.
|
||||||
|
- Játszmaelemzés – lépések listázása, hibák kiemelése.
|
||||||
|
- Webes felület vagy mobilalkalmazás támogatása.
|
||||||
|
- Játék előzményeinek és statisztikáinak megtekintése (győzelmek, vereségek, döntetlenek).
|
||||||
|
- Automatikus szervermentés és adatbázis szinkronizáció.
|
||||||
|
|
||||||
|
### Felhasználói felület
|
||||||
|
- Letisztult, reszponzív, platformfüggetlen felület (asztali és webes verzió).
|
||||||
|
- Sötét/világos téma lehetősége.
|
||||||
|
- Egyéni figurakészlet vagy tábla kinézet választása.
|
||||||
|
- Animált figuramozgások.
|
||||||
|
- Egérrel és billentyűzettel is vezérelhető játék.
|
||||||
|
- Hangjelzések a lépésekhez és az idő lejártához.
|
||||||
|
- Lépéselőzmények (move list) megjelenítése oldalt.
|
||||||
|
- Tábla forgatásának lehetősége (pl. a fehér vagy fekete nézőpontból).
|
||||||
|
- Állapotjelzők (sakk, matt, döntetlen, várakozás az ellenfélre).
|
||||||
|
- Teljes képernyős mód.
|
||||||
|
|
||||||
|
### Technikai / fejlesztői funkciók
|
||||||
|
- Kliens–szerver architektúra.
|
||||||
|
- REST API vagy WebSocket alapú kommunikáció.
|
||||||
|
- Adatbázis a felhasználói adatok és meccsek tárolására (pl. SQLite, PostgreSQL, MongoDB).
|
||||||
|
- Logolási és hibakezelési rendszer.
|
||||||
|
- Automatikus mentés és adatvisszaállítás.
|
||||||
|
- Verziókezelés (Git).
|
||||||
|
- Tesztelhető, moduláris kódszerkezet (külön modulok: logika, UI, hálózat, adat).
|
||||||
|
- Cross-platform működés (Windows, Linux, esetleg web).
|
||||||
|
|
||||||
|
### További funkciók
|
||||||
|
- Egyszemélyes mód (ember vs. gép, AI-bot).
|
||||||
|
- Több nehézségi szintű AI.
|
||||||
|
- Oktató mód (javasolt lépések, hibák magyarázata).
|
||||||
|
- Hivatalos FEN/PGN formátum export/import.
|
||||||
|
- Beépített sakkfeladványok, kihívások.
|
||||||
|
- Érintéses vezérlés mobilon.
|
||||||
|
- Többnyelvű felület (pl. magyar, angol).
|
||||||
|
|
||||||
|
## 4. Rendszer követelmények
|
||||||
|
|
||||||
|
A rendszer célja egy kétjátékos sakkalkalmazás megvalósítása, amely alapvetően hálózati kapcsolat (LAN vagy internet) segítségével biztosítja a valós idejű játékot. A rendszer kliens–szerver architektúrán alapul, ahol az egyes komponensek jól elkülönülten, meghatározott feladatokat látnak el.
|
||||||
|
|
||||||
|
### 4.1 Kötelező funkcionális követelmények
|
||||||
|
|
||||||
|
A rendszernek az alábbi alapvető funkciókat mindenképpen biztosítania kell:
|
||||||
|
- Két játékos közötti sakkjátszma lebonyolítása, a hivatalos sakk szabályai alapján.
|
||||||
|
- A játékosok felváltva tehetnek lépéseket, a lépések érvényességét a kliens oldali logika ellenőrzi.
|
||||||
|
- A rendszer valós időben szinkronizálja a két kliens állapotát (mindkét fél ugyanazt a táblát látja).
|
||||||
|
- A játék vége (sakkmatt, patt, idő lejárta) automatikusan felismerésre kerül.
|
||||||
|
- A szerver egyidejűleg több játékot is képes kezelni (külön szobákban vagy sessionökben).
|
||||||
|
- A játékosok elindíthatnak új meccset, illetve befejezett játék után visszatérhetnek a főmenübe.
|
||||||
|
- A rendszer minden játékban egyedi azonosítót (Game ID) használ a játékállapot nyomon követéséhez.
|
||||||
|
- A kliens értesítéseket kap az ellenfél lépéseiről és a játék állapotváltozásairól.
|
||||||
|
|
||||||
|
### 4.2 Kliens oldali követelmények
|
||||||
|
|
||||||
|
A kliens felelős a játékos felhasználói élményéért, a grafikus megjelenítésért és a játéklogika helyi működéséért.
|
||||||
|
|
||||||
|
A kliensnek tudnia kell:
|
||||||
|
- A sakk tábla és a figurák megjelenítése, lépések kezelése (egérkattintás vagy billentyűparancsok).
|
||||||
|
- Lépések érvényesítése és elküldése a szervernek.
|
||||||
|
- A szervertől érkező események (ellenfél lépése, állapotváltozás) feldolgozása és megjelenítése.
|
||||||
|
- Hibák és megszakadt kapcsolat kezelése (újracsatlakozási lehetőség).
|
||||||
|
- Saját IP vagy szerver cím megadása LAN esetén.
|
||||||
|
- Alapvető menürendszer (csatlakozás, szerverindítás, új játék, kilépés).
|
||||||
|
- A hálózati kommunikáció egységes formátumban történjen (pl. JSON alapú üzenetek).
|
||||||
|
|
||||||
|
### 4.3 Szerver oldali követelmények
|
||||||
|
|
||||||
|
A szerver feladata a kliensek közti kommunikáció kezelése, az állapotok szinkronizálása és a játék logikai integritásának megőrzése.
|
||||||
|
|
||||||
|
A szervernek tudnia kell:
|
||||||
|
- Kapcsolatok fogadása és kezelése több kliens esetén is.
|
||||||
|
- Új játék (session) létrehozása és azonosító kiosztása.
|
||||||
|
- Üzenetek továbbítása a kliensek között (pl. lépés, visszajelzés, játék vége).
|
||||||
|
- A játékállapot naprakészen tartása és küldése mindkét félnek.
|
||||||
|
- Kapcsolat megszakadása esetén az érintett játék szüneteltetése vagy lezárása.
|
||||||
|
- Üzenetformátumok ellenőrzése és hibás adatok elutasítása.
|
||||||
|
- Kliensazonosítás és egyszerű hitelesítés (pl. játékosnév alapján).
|
||||||
|
- A kommunikáció biztonságos kezelése (üzenetduplikáció, szinkronizációs hibák elkerülése).
|
||||||
|
|
||||||
|
### 4.4 A komponensek közti kommunikáció (szerződések)
|
||||||
|
|
||||||
|
A rendszer komponensei egy meghatározott üzenetprotokollon keresztül kommunikálnak egymással.
|
||||||
|
A kommunikáció kétirányú, valós idejű, és az alábbi szerződések szerint zajlik:
|
||||||
|
|
||||||
|
A kliens minden lépést csak akkor hajt végre a felhasználói felületen, ha a szervertől visszaigazolást kapott az érvényességéről.
|
||||||
|
A szerver az üzeneteket sorosítva, FIFO-elv szerint dolgozza fel, és broadcastolja a változásokat az adott játékhoz tartozó összes kliensnek.
|
||||||
|
|
||||||
|
### 4.5 Nem funkcionális követelmények
|
||||||
|
|
||||||
|
- Megbízhatóság: a rendszernek stabilan kell működnie hálózati késleltetés és csomagvesztés esetén is.
|
||||||
|
- Teljesítmény: a szerver legalább 10 párhuzamos játékot képes kezelni érezhető lassulás nélkül.
|
||||||
|
- Biztonság: a kliens csak a szerver által engedélyezett parancsokat hajthatja végre.
|
||||||
|
- Bővíthetőség: a rendszer felépítése moduláris legyen, hogy később könnyen kiterjeszthető legyen (pl. online matchmaking).
|
||||||
|
- Platformfüggetlenség: a kliens és a szerver futtatható legyen Windows, Linux és esetleg webes környezetben.
|
||||||
|
- Karbantarthatóság: kódmodulok (logika, hálózat, UI) elkülönítése, jól dokumentált interfészekkel.
|
||||||
|
|
||||||
|
### 4.6 Minimális technikai elvárások
|
||||||
|
|
||||||
|
- Programozási nyelv: Rust, C#, Python vagy más, hálózati alkalmazásokra alkalmas nyelv.
|
||||||
|
- Kommunikációs protokoll: TCP vagy WebSocket alapú kapcsolat.
|
||||||
|
- Adatcsere formátum: JSON.
|
||||||
|
- Grafikus felület: desktop GUI (pl. egérvezérlés, drag & drop lépés).
|
||||||
|
- Követelmény kliensoldalon: legalább 4 GB RAM, modern operációs rendszer.
|
||||||
|
- Követelmény szerveroldalon: 1 CPU mag, 512 MB RAM, állandó hálózati kapcsolat.
|
||||||
|
|
||||||
|
## 5. Követelménylista
|
||||||
|
|
||||||
|
### Szerver
|
||||||
|
| Név | Verzió | Leírás |
|
||||||
|
| --- | ------ | ------ |
|
||||||
|
| **WebSocket** | 1.0 | A szerver és a kliens között folyamatos kétirányú kommunikációt biztosít. A kapcsolat létrejötte után a szerver valós időben képes fogadni és továbbítani az eseményeket (pl. lépés végrehajtása, állapotfrissítés). Hiba esetén a kapcsolat automatikusan újraépül. |
|
||||||
|
| **Kapcsolatok csoportosítása** | 1.0 | A szerver figyeli az elérhető, szabad klienseket, majd két szabad kapcsolatot automatikusan összerendel egy meccsbe. A csoportosítás után a játékosok azonos „room”-ba kerülnek, és a szerver biztosítja az egymás közötti adatkommunikációt. |
|
||||||
|
| **Kommunikáció az engine-nel** | 1.0 | A szerver a játékosoktól érkező lépéseket és játékinformációkat továbbítja az engine-nek feldolgozásra. Az engine válasza után a szerver visszaküldi az eredményt a klienseknek (pl. érvényes lépés, matt, patt). A kommunikáció aszinkron módon zajlik, válaszidő-ellenőrzéssel. |
|
||||||
|
| **Kommunikáció a UI-al** | 1.0 | A szerver WebSocket-en keresztül adatokat továbbít a felhasználói felület és az engine között. A UI által kért műveletek (pl. új meccs létrehozása, állapotlekérés) feldolgozását a szerver közvetíti.|
|
||||||
|
### Engine
|
||||||
|
| Név | Verzió | Leírás |
|
||||||
|
| --- | ------ | ------ |
|
||||||
|
| **Bitboard** | 1.0 | A játék táblaállapotát bitműveletekkel reprezentálja a hatékonyság érdekében. Minden bábu típus és szín külön bitmask-on kerül tárolásra, lehetővé téve a gyors lekérdezéseket és lépésellenőrzéseket. |
|
||||||
|
| **Lépésgenerálás LUT** | 1.0 | Előre kiszámított lookup táblák segítségével gyorsítja a lépésgenerálást és szabályellenőrzést. Ez csökkenti a számítási időt, és optimalizálja az engine teljesítményét. |
|
||||||
|
| **Lépésgenerálás** | 1.0 | A különböző bábutípusok (gyalog, bástya, futó, stb.) lépési logikáját valósítja meg. A függvények ellenőrzik a lépés érvényességét, figyelembe véve az aktuális állást, sakkhelyzetet és speciális szabályokat (pl. sáncolás, en passant). |
|
||||||
|
| **Util függvények** | 1.0 | Segédfüggvények az engine belső működéséhez, például raycast műveletek, bitműveleti maszkok kezelése, valamint logikai ellenőrzések a lépések és ütések számításához. |
|
||||||
|
|
||||||
|
### UI
|
||||||
|
| Név | Verzió | Leírás |
|
||||||
|
| --- | ------ | ------ |
|
||||||
|
| **Belépés** | 1.0 | A felhasználó a kezdőképernyőn keresztül adhatja meg a nevét lokális játékhoz, vagy hitelesítheti magát online játékmód esetén. Hibás adatok esetén a rendszer figyelmeztetést küld. |
|
||||||
|
| **Főmenü** | 1.0 | Az alkalmazás központi navigációs felülete, ahol a felhasználó meccset kereshet, új játékot indíthat lokálisan, vagy beállításokat módosíthat. A menü megjeleníti az aktuális státuszt (online/offline). |
|
||||||
|
| **Játék** | 1.0 | A játékfelület megjeleníti a táblát, bábukat, lépéseket, és az aktuális játékállást. Támogatja mind az online, mind a lokális módot. A felület kezeli az interakciókat (lépéskattintás, visszavonás, végeredmény kijelzés). |
|
||||||
|
| **Kommunikáció a szerverrel** | 1.0 | A kliens a szerveren keresztül kommunikál az engine-nel. A UI felel az üzenetek küldéséért (lépés, új játék, visszajelzés), valamint a szervertől kapott események vizuális megjelenítéséért. |
|
||||||
|
|
||||||
|
|
||||||
|
### GitHub Actions (CI/CD)
|
||||||
|
| Név | Leírás |
|
||||||
|
| --- | ------ |
|
||||||
|
| Folyamatos tesztelés | A projekt minden commit után automatikusan tesztelődik. A pipeline lefuttatja a teszteket, és értesítést küld hibás build esetén. |
|
||||||
|
| Folyamatos integráció | Az új funkciók beolvadásakor a rendszer automatikusan integrálja a változtatásokat, új buildet hoz létre, és frissíti a fejlesztői környezetet. |
|
||||||
|
| Tesztadatok | A tesztadatok legyenek elérhetőek egy táblázatban, dátummal ellátva. (Google Sheets) |
|
||||||
@@ -4,3 +4,6 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
once_cell = "1.19"
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
|||||||
11
engine/src/bitboard.rs
Normal file
11
engine/src/bitboard.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
mod attackmaps;
|
||||||
|
mod utils;
|
||||||
|
mod legality;
|
||||||
|
mod checkinfo;
|
||||||
|
mod attacks;
|
||||||
|
mod movegen;
|
||||||
|
|
||||||
|
pub mod board;
|
||||||
|
pub(in super) mod bitmove;
|
||||||
|
pub(in super) mod movebuffer;
|
||||||
|
pub(in super) mod makemove;
|
||||||
310
engine/src/bitboard/attackmaps.rs
Normal file
310
engine/src/bitboard/attackmaps.rs
Normal file
@@ -0,0 +1,310 @@
|
|||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
|
const A_FILE: u64 = 0x0101_0101_0101_0101;
|
||||||
|
const H_FILE: u64 = 0x8080_8080_8080_8080;
|
||||||
|
const AB_FILE: u64 = 0x0303_0303_0303_0303;
|
||||||
|
const GH_FILE: u64 = 0xC0C0_C0C0_C0C0_C0C0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
EXPLANATIONS:
|
||||||
|
> square_index: 8 * rank number + file number (a-h = 0-7)
|
||||||
|
> side: white = 0, black = 1
|
||||||
|
> direction_index: 0..8 = [E, NE, N, NW, W, SW, S, SE]
|
||||||
|
*/
|
||||||
|
|
||||||
|
// KING_ATTACK_MAP[<square_index>]
|
||||||
|
pub static KING_ATTACK_MAP: Lazy<[u64; 64]> = Lazy::new(|| {
|
||||||
|
let mut table: [u64; 64] = [0u64; 64];
|
||||||
|
|
||||||
|
for sq in 0..64 {
|
||||||
|
let king: u64 = 1 << sq;
|
||||||
|
|
||||||
|
let left_attacks: u64 = king << 7 | king >> 1 | king >> 9;
|
||||||
|
let right_attacks: u64 = king << 1 | king << 9 | king >> 7;
|
||||||
|
|
||||||
|
table[sq] = (left_attacks & !H_FILE) | (right_attacks & !A_FILE) | king << 8 | king >> 8;
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
});
|
||||||
|
|
||||||
|
// PAWN_ATTACK_MAP[<square_index>][<side>]
|
||||||
|
pub static PAWN_ATTACK_MAP: Lazy<[[u64; 2]; 64]> = Lazy::new(|| {
|
||||||
|
let mut table: [[u64; 2]; 64] = [[0u64; 2]; 64];
|
||||||
|
|
||||||
|
for sq in 0..64 {
|
||||||
|
let pawn: u64 = 1 << sq;
|
||||||
|
table[sq][0] |= (pawn << 9) & !A_FILE;
|
||||||
|
table[sq][0] |= (pawn << 7) & !H_FILE;
|
||||||
|
}
|
||||||
|
for sq in 0..64 {
|
||||||
|
let pawn: u64 = 1 << sq;
|
||||||
|
table[sq][1] |= (pawn >> 9) & !H_FILE;
|
||||||
|
table[sq][1] |= (pawn >> 7) & !A_FILE;
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
});
|
||||||
|
|
||||||
|
// KNIGHT_ATTACK_MAP[<square_index>]
|
||||||
|
pub static KNIGHT_ATTACK_MAP: Lazy<[u64; 64]> = Lazy::new(|| {
|
||||||
|
let mut table: [u64; 64] = [0u64; 64];
|
||||||
|
|
||||||
|
for sq in 0..64 {
|
||||||
|
let knight: u64 = 1 << sq;
|
||||||
|
|
||||||
|
let far_left_attacks: u64 = knight << 6 | knight >> 10;
|
||||||
|
let near_left_attacks: u64 = knight << 15 | knight >> 17;
|
||||||
|
let far_right_attacks: u64 = knight << 10 | knight >> 6;
|
||||||
|
let near_right_attacks: u64 = knight << 17 | knight >> 15;
|
||||||
|
|
||||||
|
table[sq] = (far_left_attacks & !GH_FILE) | (far_right_attacks & !AB_FILE) | (near_left_attacks & !H_FILE) | (near_right_attacks & !A_FILE);
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
});
|
||||||
|
|
||||||
|
// RAY_TABLE[<square_index>][<direction_index>]
|
||||||
|
pub static RAY_TABLE: Lazy<[[u64; 8]; 64]> = Lazy::new(|| {
|
||||||
|
let mut table: [[u64; 8]; 64] = [[0u64; 8]; 64];
|
||||||
|
let dirs: [i8; 8] = [1, 9, 8, 7, -1, -9, -8, -7];
|
||||||
|
for sq in 0..64 {
|
||||||
|
for d in 0..8 {
|
||||||
|
let mut ray: u64 = 0u64;
|
||||||
|
let origin: u64 = 1 << sq;
|
||||||
|
let mut new_target: u64 = if dirs[d] > 0 {origin << dirs[d]} else {origin >> -dirs[d]};
|
||||||
|
if [0, 1, 7].contains(&d) {
|
||||||
|
new_target &= !A_FILE;
|
||||||
|
}
|
||||||
|
else if [3, 4, 5].contains(&d) {
|
||||||
|
new_target &= !H_FILE;
|
||||||
|
}
|
||||||
|
while new_target != 0 {
|
||||||
|
ray |= new_target;
|
||||||
|
|
||||||
|
new_target = if dirs[d] > 0 {new_target << dirs[d]} else {new_target >> -dirs[d]};
|
||||||
|
if [0, 1, 7].contains(&d) {
|
||||||
|
new_target &= !A_FILE;
|
||||||
|
}
|
||||||
|
else if [3, 4, 5].contains(&d) {
|
||||||
|
new_target &= !H_FILE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
table[sq][d] = ray;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return table;
|
||||||
|
});
|
||||||
|
|
||||||
|
// ROOK_MOVE_MASK[<square_index>]
|
||||||
|
pub static ROOK_MOVE_MASK: Lazy<[u64; 64]> = Lazy::new(|| {
|
||||||
|
let mut table = [0u64; 64];
|
||||||
|
|
||||||
|
for sq in 0..64 {
|
||||||
|
for dir in [0, 2, 4, 6] {
|
||||||
|
table[sq] |= RAY_TABLE[sq][dir];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
table
|
||||||
|
});
|
||||||
|
|
||||||
|
// BISHOP_MOVE_MASK[<square_index>]
|
||||||
|
pub static BISHOP_MOVE_MASK: Lazy<[u64; 64]> = Lazy::new(|| {
|
||||||
|
let mut table = [0u64; 64];
|
||||||
|
|
||||||
|
for sq in 0..64 {
|
||||||
|
for dir in [1, 3, 5, 7] {
|
||||||
|
table[sq] |= RAY_TABLE[sq][dir];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
table
|
||||||
|
});
|
||||||
|
|
||||||
|
// KING_SAFETY_ROOK_MASK[<square_index>]
|
||||||
|
pub static KING_SAFETY_ROOK_MASK: Lazy<[u64; 64]> = Lazy::new(|| {
|
||||||
|
let mut table = [0u64; 64];
|
||||||
|
|
||||||
|
for sq in 0..64 {
|
||||||
|
let mut mask = KING_ATTACK_MAP[sq];
|
||||||
|
|
||||||
|
while mask != 0 {
|
||||||
|
let next_sq = mask.trailing_zeros();
|
||||||
|
table[sq] |= ROOK_MOVE_MASK[next_sq as usize];
|
||||||
|
mask &= !(1 << next_sq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
table
|
||||||
|
});
|
||||||
|
|
||||||
|
// KING_SAFETY_BISHOP_MASK[<square_index>]
|
||||||
|
pub static KING_SAFETY_BISHOP_MASK: Lazy<[u64; 64]> = Lazy::new(|| {
|
||||||
|
let mut table = [0u64; 64];
|
||||||
|
|
||||||
|
for sq in 0..64 {
|
||||||
|
let mut mask = KING_ATTACK_MAP[sq];
|
||||||
|
|
||||||
|
while mask != 0 {
|
||||||
|
let next_sq = mask.trailing_zeros();
|
||||||
|
table[sq] |= BISHOP_MOVE_MASK[next_sq as usize];
|
||||||
|
mask &= !(1 << next_sq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
table
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// <----- TESTS ----->
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_king_attack_map() {
|
||||||
|
|
||||||
|
// test setup for corners [SW, SE, NW, NE]
|
||||||
|
let corner_indexes: [usize; 4] = [0, 7, 56, 63];
|
||||||
|
let corner_attack_maps: [u64; 4] = [
|
||||||
|
(1u64 << 1) | (1u64 << 8) | (1u64 << 9),
|
||||||
|
(1u64 << 6) | (1u64 << 14) | (1u64 << 15),
|
||||||
|
(1u64 << 48) | (1u64 << 49) | (1u64 << 57),
|
||||||
|
(1u64 << 54) | (1u64 << 55) | (1u64 << 62)
|
||||||
|
];
|
||||||
|
|
||||||
|
// tests for corners
|
||||||
|
for index in 0..4 {
|
||||||
|
assert_eq!(KING_ATTACK_MAP[corner_indexes[index]], corner_attack_maps[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// test setup for sides [S, E, W, N]
|
||||||
|
let side_indexes: [usize; 4] = [3, 31, 32, 60];
|
||||||
|
let side_attack_maps: [u64; 4] = [
|
||||||
|
(1 << 2) | (1 << 4) | (1 << 10) | (1 << 11) | (1 << 12),
|
||||||
|
(1 << 22) | (1 << 23) | (1 << 30) | (1 << 38) | (1 << 39),
|
||||||
|
(1 << 24) | (1 << 25) | (1 << 33) | (1 << 40) | (1 << 41),
|
||||||
|
(1 << 51) | (1 << 52) | (1 << 53) | (1 << 59) | (1 << 61)
|
||||||
|
];
|
||||||
|
|
||||||
|
// tests for sides
|
||||||
|
for index in 0..4 {
|
||||||
|
assert_eq!(KING_ATTACK_MAP[side_indexes[index]], side_attack_maps[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// test setup for center
|
||||||
|
let center_index: usize = 27;
|
||||||
|
let center_attack_map: u64 = (1 << 18) | (1 << 19) | (1 << 20) | (1 << 26) | (1 << 28) | (1 << 34) | (1 << 35) | (1 << 36);
|
||||||
|
|
||||||
|
// test for center
|
||||||
|
assert_eq!(KING_ATTACK_MAP[center_index], center_attack_map);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_pawn_attack_map() {
|
||||||
|
|
||||||
|
// test setup for white sides
|
||||||
|
let white_side_indexes: [usize; 2] = [24, 31];
|
||||||
|
let white_side_attack_maps: [u64; 2] = [
|
||||||
|
(1 << 33),
|
||||||
|
(1 << 38)
|
||||||
|
];
|
||||||
|
// tests for white sides
|
||||||
|
for index in 0..2 {
|
||||||
|
assert_eq!(PAWN_ATTACK_MAP[white_side_indexes[index]][0], white_side_attack_maps[index])
|
||||||
|
}
|
||||||
|
|
||||||
|
// test setup for black sides
|
||||||
|
let black_side_indexes: [usize; 2] = [32, 39];
|
||||||
|
let black_side_attack_maps: [u64; 2] = [
|
||||||
|
(1 << 25),
|
||||||
|
(1 << 30)
|
||||||
|
];
|
||||||
|
// tests for black sides
|
||||||
|
for index in 0..2 {
|
||||||
|
assert_eq!(PAWN_ATTACK_MAP[black_side_indexes[index]][1], black_side_attack_maps[index])
|
||||||
|
}
|
||||||
|
|
||||||
|
// test setup for white center
|
||||||
|
let white_center_indexes: [usize; 2] = [11, 12];
|
||||||
|
let white_center_attack_maps: [u64; 2] = [
|
||||||
|
(1 << 18) | (1 << 20),
|
||||||
|
(1 << 19) | (1 << 21)
|
||||||
|
];
|
||||||
|
// tests for white center
|
||||||
|
for index in 0..2 {
|
||||||
|
assert_eq!(PAWN_ATTACK_MAP[white_center_indexes[index]][0], white_center_attack_maps[index])
|
||||||
|
}
|
||||||
|
|
||||||
|
// test setup for black center
|
||||||
|
let black_center_indexes: [usize; 2] = [51, 52];
|
||||||
|
let black_center_attack_maps: [u64; 2] = [
|
||||||
|
(1 << 42) | (1 << 44),
|
||||||
|
(1 << 43) | (1 << 45)
|
||||||
|
];
|
||||||
|
// tests for black center
|
||||||
|
for index in 0..2 {
|
||||||
|
assert_eq!(PAWN_ATTACK_MAP[black_center_indexes[index]][1], black_center_attack_maps[index])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_knight_attack_map() {
|
||||||
|
// test setup for corners [SW, SE, NW, NE]
|
||||||
|
let corner_indexes: [usize; 4] = [0, 7, 56, 63];
|
||||||
|
let corner_attack_maps: [u64; 4] = [
|
||||||
|
(1 << 17) | (1 << 10),
|
||||||
|
(1 << 13) | (1 << 22),
|
||||||
|
(1 << 41) | (1 << 50),
|
||||||
|
(1 << 46) | (1 << 53)
|
||||||
|
];
|
||||||
|
|
||||||
|
// tests for corners
|
||||||
|
for index in 0..4 {
|
||||||
|
assert_eq!(KNIGHT_ATTACK_MAP[corner_indexes[index]], corner_attack_maps[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// test setup for sides [S, E, W, N]
|
||||||
|
let side_indexes: [usize; 4] = [3, 31, 32, 60];
|
||||||
|
let side_attack_maps: [u64; 4] = [
|
||||||
|
(1 << 9) | (1 << 13) | (1 << 18) | (1 << 20),
|
||||||
|
(1 << 14) | (1 << 21) | (1 << 37) | (1 << 46),
|
||||||
|
(1 << 17) | (1 << 26) | (1 << 42) | (1 << 49),
|
||||||
|
(1 << 43) | (1 << 45) | (1 << 50) | (1 << 54)
|
||||||
|
];
|
||||||
|
|
||||||
|
// tests for sides
|
||||||
|
for index in 0..4 {
|
||||||
|
assert_eq!(KNIGHT_ATTACK_MAP[side_indexes[index]], side_attack_maps[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// test setup for center
|
||||||
|
let center_index: usize = 27;
|
||||||
|
let center_attack_map: u64 = (1 << 10) | (1 << 12) | (1 << 17) | (1 << 21) | (1 << 33) | (1 << 37) | (1 << 42) | (1 << 44);
|
||||||
|
|
||||||
|
// test for center
|
||||||
|
assert_eq!(KNIGHT_ATTACK_MAP[center_index], center_attack_map);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ray_table() {
|
||||||
|
|
||||||
|
// test setup for all directions from center
|
||||||
|
let starting_square_index: usize = 27;
|
||||||
|
let ray_masks: [u64; 8] = [
|
||||||
|
(1 << 28) | (1 << 29) | (1 << 30) | (1 << 31),
|
||||||
|
(1 << 36) | (1 << 45) | (1 << 54) | (1 << 63),
|
||||||
|
(1 << 35) | (1 << 43) | (1 << 51) | (1 << 59),
|
||||||
|
(1 << 34) | (1 << 41) | (1 << 48),
|
||||||
|
(1 << 26) | (1 << 25) | (1 << 24),
|
||||||
|
(1 << 18) | (1 << 9) | (1 << 0),
|
||||||
|
(1 << 19) | (1 << 11) | (1 << 3),
|
||||||
|
(1 << 20) | (1 << 13) | (1 << 6)
|
||||||
|
];
|
||||||
|
|
||||||
|
// tests for all directions from starting_square
|
||||||
|
for direction in 0..8 {
|
||||||
|
assert_eq!(RAY_TABLE[starting_square_index][direction], ray_masks[direction]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
196
engine/src/bitboard/attacks.rs
Normal file
196
engine/src/bitboard/attacks.rs
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
use super::board::Board;
|
||||||
|
use super::attackmaps::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
const RANK_2: u64 = 0x0000_0000_0000_FF00;
|
||||||
|
const RANK_7: u64 = 0x00FF_0000_0000_0000;
|
||||||
|
const A_FILE: u64 = 0x0101_0101_0101_0101;
|
||||||
|
const H_FILE: u64 = 0x8080_8080_8080_8080;
|
||||||
|
|
||||||
|
pub fn get_pseudo_pawn_moves(&self, sq: u32) -> u64 {
|
||||||
|
let pawn: u64 = 1 << sq;
|
||||||
|
let mut move_mask: u64 = 0u64;
|
||||||
|
let move_offset: i8 = 8 - 16 * self.side_to_move as i8;
|
||||||
|
|
||||||
|
let next_sq: u64 = if move_offset > 0 {pawn << move_offset} else {pawn >> -move_offset};
|
||||||
|
if (self.occupancy[2] & next_sq) == 0 {
|
||||||
|
move_mask |= next_sq;
|
||||||
|
|
||||||
|
if (self.side_to_move == 0 && pawn & Self::RANK_2 != 0)
|
||||||
|
|| (self.side_to_move == 1 && pawn & Self::RANK_7 != 0) {
|
||||||
|
|
||||||
|
let next_sq: u64 = if move_offset > 0 {next_sq << move_offset} else {next_sq >> -move_offset};
|
||||||
|
if (self.occupancy[2] & next_sq) == 0 {
|
||||||
|
move_mask |= next_sq;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return move_mask;
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn get_pseudo_knight_moves(&self, sq: u32) -> u64 {
|
||||||
|
return KNIGHT_ATTACK_MAP[sq as usize];
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn get_pseudo_king_moves(&self, sq: u32) -> u64 {
|
||||||
|
return KING_ATTACK_MAP[sq as usize];
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn get_pseudo_pawn_captures(&self, sq: u32) -> u64 {
|
||||||
|
return PAWN_ATTACK_MAP[sq as usize][self.side_to_move as usize];
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn get_pseudo_opponent_pawn_captures(&self, sq: u32) -> u64 {
|
||||||
|
return PAWN_ATTACK_MAP[sq as usize][1 - self.side_to_move as usize];
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn get_pseudo_bishop_moves(&self, sq: u32) -> u64 {
|
||||||
|
let mut moves = 0u64;
|
||||||
|
let sq = sq as usize;
|
||||||
|
let occupancy = self.occupancy[2];
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 1);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 3);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 5);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 7);
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn get_pseudo_rook_moves(&self, sq: u32) -> u64 {
|
||||||
|
let mut moves: u64 = 0u64;
|
||||||
|
let occupancy = self.occupancy[2];
|
||||||
|
let sq = sq as usize;
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 0);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 2);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 4);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 6);
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn get_pseudo_queen_moves(&self, sq: u32) -> u64 {
|
||||||
|
return self.get_pseudo_bishop_moves(sq) | self.get_pseudo_rook_moves(sq);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_pseudo_bishop_moves_ignore_king(&self, sq: u32) -> u64 {
|
||||||
|
let mut moves = 0u64;
|
||||||
|
let sq = sq as usize;
|
||||||
|
let king = self.bitboards[5 + 6*self.side_to_move as usize];
|
||||||
|
let occupancy = self.occupancy[2] & !king;
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 1);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 3);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 5);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 7);
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn get_pseudo_rook_moves_ignore_king(&self, sq: u32) -> u64 {
|
||||||
|
let mut moves: u64 = 0u64;
|
||||||
|
let sq = sq as usize;
|
||||||
|
let king = self.bitboards[5 + 6*self.side_to_move as usize];
|
||||||
|
let occupancy = self.occupancy[2] & !king;
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 0);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 2);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 4);
|
||||||
|
moves |= get_raycast_from_square_in_direction(occupancy, sq, 6);
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_square_attacked(&self, king_sq: u32) -> bool {
|
||||||
|
let offset: usize = 6 * self.side_to_move as usize;
|
||||||
|
|
||||||
|
// rook-queen checks (+)
|
||||||
|
let mut threat_mask: u64 = self.get_pseudo_rook_moves(king_sq);
|
||||||
|
let mut attacker_mask: u64 = self.bitboards[10 - offset] | self.bitboards[9 - offset];
|
||||||
|
if threat_mask & attacker_mask != 0 { return true; }
|
||||||
|
|
||||||
|
// bishop-queen checks (x)
|
||||||
|
threat_mask = self.get_pseudo_bishop_moves(king_sq);
|
||||||
|
attacker_mask = self.bitboards[10 - offset] | self.bitboards[8 - offset];
|
||||||
|
if threat_mask & attacker_mask != 0 { return true; }
|
||||||
|
|
||||||
|
// knight checks (L)
|
||||||
|
threat_mask = KNIGHT_ATTACK_MAP[king_sq as usize];
|
||||||
|
attacker_mask = self.bitboards[7 - offset];
|
||||||
|
if threat_mask & attacker_mask != 0 { return true; }
|
||||||
|
|
||||||
|
// pawn checks (v)
|
||||||
|
threat_mask = PAWN_ATTACK_MAP[king_sq as usize][self.side_to_move as usize];
|
||||||
|
attacker_mask = self.bitboards[6 - offset];
|
||||||
|
return threat_mask & attacker_mask != 0;
|
||||||
|
}
|
||||||
|
pub fn get_safe_king_squares(&self) -> u64 {
|
||||||
|
let offset: usize = 6 * (1 - self.side_to_move as usize);
|
||||||
|
let king_sq = self.bitboards[11 - offset].trailing_zeros() as usize;
|
||||||
|
let bishop_mask = KING_SAFETY_BISHOP_MASK[king_sq];
|
||||||
|
let rook_mask = KING_SAFETY_ROOK_MASK[king_sq];
|
||||||
|
let mut attack_map: u64 = 0u64;
|
||||||
|
|
||||||
|
let mut board: u64 = self.bitboards[offset];
|
||||||
|
if self.side_to_move() == 0 {
|
||||||
|
attack_map |= (board >> 9 & !Self::H_FILE) | (board >> 7 & !Self::A_FILE);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
attack_map |= (board << 9 & !Self::A_FILE) | (board << 7 & !Self::H_FILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
board = self.bitboards[offset + 1];
|
||||||
|
while board != 0 {
|
||||||
|
let piece_sq: u32 = board.trailing_zeros();
|
||||||
|
board &= !(1 << piece_sq);
|
||||||
|
|
||||||
|
attack_map |= self.get_pseudo_knight_moves(piece_sq);
|
||||||
|
}
|
||||||
|
|
||||||
|
board = self.bitboards[offset + 2] & bishop_mask;
|
||||||
|
while board != 0 {
|
||||||
|
let piece_sq: u32 = board.trailing_zeros();
|
||||||
|
board &= !(1 << piece_sq);
|
||||||
|
|
||||||
|
attack_map |= self.get_pseudo_bishop_moves_ignore_king(piece_sq);
|
||||||
|
}
|
||||||
|
|
||||||
|
board = self.bitboards[offset + 3] & rook_mask;
|
||||||
|
while board != 0 {
|
||||||
|
let piece_sq: u32 = board.trailing_zeros();
|
||||||
|
board &= !(1 << piece_sq);
|
||||||
|
|
||||||
|
attack_map |= self.get_pseudo_rook_moves_ignore_king(piece_sq);
|
||||||
|
}
|
||||||
|
|
||||||
|
board = self.bitboards[offset + 4] & (bishop_mask | rook_mask);
|
||||||
|
while board != 0 {
|
||||||
|
let piece_sq: u32 = board.trailing_zeros();
|
||||||
|
board &= !(1 << piece_sq);
|
||||||
|
|
||||||
|
attack_map |= self.get_pseudo_rook_moves_ignore_king(piece_sq) | self.get_pseudo_bishop_moves_ignore_king(piece_sq);
|
||||||
|
}
|
||||||
|
|
||||||
|
board = self.bitboards[offset + 5];
|
||||||
|
let piece_sq: u32 = board.trailing_zeros();
|
||||||
|
attack_map |= self.get_pseudo_king_moves(piece_sq);
|
||||||
|
|
||||||
|
return !attack_map;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn get_raycast_from_square_in_direction(occupancy: u64, sq: usize, dir: usize) -> u64 {
|
||||||
|
let is_up: bool = dir / 4 == 0;
|
||||||
|
let mut ray: u64 = RAY_TABLE[sq][dir];
|
||||||
|
let blockers: u64 = occupancy & ray;
|
||||||
|
|
||||||
|
if blockers != 0 {
|
||||||
|
let first_blocker: u32 = if is_up { blockers.trailing_zeros() } else { 63 - blockers.leading_zeros() };
|
||||||
|
|
||||||
|
ray &= !RAY_TABLE[first_blocker as usize][dir];
|
||||||
|
}
|
||||||
|
|
||||||
|
return ray;
|
||||||
|
}
|
||||||
77
engine/src/bitboard/bitmove.rs
Normal file
77
engine/src/bitboard/bitmove.rs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
use super::utils::*;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub struct BitMove {
|
||||||
|
|
||||||
|
move_type: BitMoveType,
|
||||||
|
from_square: u8,
|
||||||
|
to_square: u8,
|
||||||
|
promotion_piece: Option<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BitMove {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn quiet(from: u8, to: u8, promotion_piece: Option<u8>) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: BitMoveType::Quiet,
|
||||||
|
from_square: from,
|
||||||
|
to_square: to,
|
||||||
|
promotion_piece: promotion_piece
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn capture(from: u8, to: u8, promotion_piece: Option<u8>) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: BitMoveType::Capture,
|
||||||
|
from_square: from,
|
||||||
|
to_square: to,
|
||||||
|
promotion_piece: promotion_piece
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn castle(from: u8, to: u8) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: BitMoveType::Castle,
|
||||||
|
from_square: from,
|
||||||
|
to_square: to,
|
||||||
|
promotion_piece: None
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn move_type(&self) -> BitMoveType {
|
||||||
|
return self.move_type;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn from_square(&self) -> u8 {
|
||||||
|
return self.from_square;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn to_square(&self) -> u8 {
|
||||||
|
return self.to_square;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn promotion_piece(&self) -> Option<u8> {
|
||||||
|
return self.promotion_piece;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn uci_notation(&self) -> String {
|
||||||
|
let mut notation = notation_from_square_number(self.from_square());
|
||||||
|
notation.push_str(¬ation_from_square_number(self.to_square()));
|
||||||
|
|
||||||
|
if let Some(promotion_piece) = self.promotion_piece {
|
||||||
|
notation.push(get_character_by_piece_id(promotion_piece).to_ascii_lowercase());
|
||||||
|
}
|
||||||
|
|
||||||
|
return notation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum BitMoveType {
|
||||||
|
Quiet,
|
||||||
|
Capture,
|
||||||
|
Castle,
|
||||||
|
EnPassant
|
||||||
|
}
|
||||||
295
engine/src/bitboard/board.rs
Normal file
295
engine/src/bitboard/board.rs
Normal file
@@ -0,0 +1,295 @@
|
|||||||
|
use crate::bitboard::utils::notation_from_square_number;
|
||||||
|
|
||||||
|
use super::utils::try_get_square_number_from_notation;
|
||||||
|
|
||||||
|
pub struct Board {
|
||||||
|
pub(in super) bitboards: [u64; 12], // 0-5 -> white pieces (P, N, B, R, Q, K), 6-11 -> black pieces (p, n, b, r, q, k)
|
||||||
|
pub(in super) piece_board: [u8; 64], // same as board indexes, 12 -> empty square
|
||||||
|
pub(in super) occupancy: [u64; 3], // 0 -> white, 1 -> black, 2 -> combined
|
||||||
|
pub(in super) castling_rights: u8, // 0b0000_KQkq
|
||||||
|
pub(in super) pinned_squares: [u8; 64], // 0 -> E-W, 1 -> NE-SW, 2 -> N-S, 3 -> SE-NW, 4 -> no pin
|
||||||
|
pub(in super) pin_mask: u64, // 1 -> pin, 0 -> no pin
|
||||||
|
pub(in super) en_passant_square: u64, // 1 -> ep square, 0 -> no ep square
|
||||||
|
pub(in super) side_to_move: u8 // 0 -> white to play, 1 -> black to play
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
pub const EMPTY_SQUARE: u8 = 12;
|
||||||
|
|
||||||
|
pub fn new_clear() -> Self {
|
||||||
|
let mut bit_board: Self = Self {
|
||||||
|
bitboards: [0x0000_0000_0000_0000; 12],
|
||||||
|
piece_board: [12; 64],
|
||||||
|
occupancy: [0x0000_0000_0000_0000; 3],
|
||||||
|
castling_rights: 0b0000_0000,
|
||||||
|
pinned_squares: [4; 64],
|
||||||
|
pin_mask: 0u64,
|
||||||
|
en_passant_square: 0x0000_0000_0000_0000,
|
||||||
|
side_to_move: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
return bit_board;
|
||||||
|
}
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let mut bit_board: Board = Self {
|
||||||
|
bitboards: [0x0000_0000_0000_FF00,
|
||||||
|
0x0000_0000_0000_0042,
|
||||||
|
0x0000_0000_0000_0024,
|
||||||
|
0x0000_0000_0000_0081,
|
||||||
|
0x0000_0000_0000_0008,
|
||||||
|
0x0000_0000_0000_0010,
|
||||||
|
0x00FF_0000_0000_0000,
|
||||||
|
0x4200_0000_0000_0000,
|
||||||
|
0x2400_0000_0000_0000,
|
||||||
|
0x8100_0000_0000_0000,
|
||||||
|
0x0800_0000_0000_0000,
|
||||||
|
0x1000_0000_0000_0000],
|
||||||
|
piece_board: [12; 64],
|
||||||
|
occupancy: [0; 3],
|
||||||
|
castling_rights: 0b0000_1111,
|
||||||
|
pinned_squares: [4; 64],
|
||||||
|
pin_mask: 0u64,
|
||||||
|
en_passant_square: 0x0000_0000_0000_0000,
|
||||||
|
side_to_move: 0
|
||||||
|
};
|
||||||
|
bit_board.calc_occupancy();
|
||||||
|
bit_board.calc_piece_board();
|
||||||
|
|
||||||
|
return bit_board;
|
||||||
|
}
|
||||||
|
pub fn build(fen: &str) -> Self {
|
||||||
|
let mut board: Board = Board::new_clear();
|
||||||
|
|
||||||
|
let mut col: i32 = 0;
|
||||||
|
let mut row: i32 = 7;
|
||||||
|
let pieces: [char; 12] = ['p', 'n', 'b', 'r', 'q', 'k', 'P', 'N', 'B', 'R', 'Q', 'K'];
|
||||||
|
let mut coming_up: &str = fen;
|
||||||
|
|
||||||
|
for (i, c) in coming_up.chars().enumerate() {
|
||||||
|
if pieces.contains(&c) {
|
||||||
|
board.place_piece(row*8 + col, c);
|
||||||
|
col += 1;
|
||||||
|
}
|
||||||
|
else if ('1'..='8').contains(&c) {
|
||||||
|
col += c.to_string().parse::<i32>().unwrap();
|
||||||
|
}
|
||||||
|
else if c == '/' {
|
||||||
|
row -= 1;
|
||||||
|
col = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
coming_up = &coming_up[i+1..];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
board.calc_occupancy();
|
||||||
|
|
||||||
|
match coming_up.chars().next().unwrap() {
|
||||||
|
'w' => board.side_to_move = 0,
|
||||||
|
'b' => board.side_to_move = 1,
|
||||||
|
_ => panic!("invalid fen notation / to be handled later")
|
||||||
|
}
|
||||||
|
coming_up = &coming_up[2..];
|
||||||
|
|
||||||
|
for (i, c) in coming_up.chars().enumerate() {
|
||||||
|
match c {
|
||||||
|
'K' => board.castling_rights |= 1 << 3,
|
||||||
|
'Q' => board.castling_rights |= 1 << 2,
|
||||||
|
'k' => board.castling_rights |= 1 << 1,
|
||||||
|
'q' => board.castling_rights |= 1,
|
||||||
|
'-' => {
|
||||||
|
coming_up = &coming_up[i+2..];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
coming_up = &coming_up[i+1..];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
match coming_up.chars().next().unwrap() {
|
||||||
|
'-' => {
|
||||||
|
coming_up = &coming_up[1..];
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let notation = coming_up.split(' ').next().unwrap();
|
||||||
|
if let Ok(epsq_index) = try_get_square_number_from_notation(notation) {
|
||||||
|
board.en_passant_square = 1 << epsq_index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
board.calc_pinned_squares();
|
||||||
|
board.calc_piece_board();
|
||||||
|
|
||||||
|
return board;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn bitboards(&self, index: usize) -> u64 {
|
||||||
|
return self.bitboards[index];
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn piece_board(&self, sq: u8) -> u8 {
|
||||||
|
return self.piece_board[sq as usize];
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn occupancy(&self, side: usize) -> u64 {
|
||||||
|
return self.occupancy[side];
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn castling_rights(&self) -> u8 {
|
||||||
|
return self.castling_rights;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn pinned_squares(&self, sq: usize) -> u8 {
|
||||||
|
return self.pinned_squares[sq];
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn pin_mask(&self) -> u64 {
|
||||||
|
return self.pin_mask;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn en_passant_square(&self) -> u64 {
|
||||||
|
return self.en_passant_square;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn side_to_move(&self) -> u8 {
|
||||||
|
return self.side_to_move;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn current_king_square(&self) -> u32 {
|
||||||
|
return if self.side_to_move == 0 { self.bitboards[5].trailing_zeros() } else { self.bitboards[11].trailing_zeros() };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fen(&self) -> String {
|
||||||
|
let mut fen = String::new();
|
||||||
|
|
||||||
|
for row in (0..8).rev() {
|
||||||
|
let mut empty = 0;
|
||||||
|
for col in 0..8 {
|
||||||
|
let sq = row * 8 + col;
|
||||||
|
if let Some(piece) = self.get_piece_character(sq) {
|
||||||
|
if empty > 0 {
|
||||||
|
fen.push_str(&empty.to_string());
|
||||||
|
empty = 0;
|
||||||
|
}
|
||||||
|
fen.push(piece);
|
||||||
|
} else {
|
||||||
|
empty += 1;
|
||||||
|
if col == 7 {
|
||||||
|
fen.push_str(&empty.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if row > 0 {
|
||||||
|
fen.push('/');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fen.push(' ');
|
||||||
|
if self.side_to_move() == 0 { fen.push('w'); } else { fen.push('b'); }
|
||||||
|
|
||||||
|
fen.push(' ');
|
||||||
|
if self.castling_rights() == 0 {
|
||||||
|
fen.push('-');
|
||||||
|
} else {
|
||||||
|
if self.castling_rights() & (1 << 3) != 0 { fen.push('K'); }
|
||||||
|
if self.castling_rights() & (1 << 2) != 0 { fen.push('Q'); }
|
||||||
|
if self.castling_rights() & (1 << 1) != 0 { fen.push('k'); }
|
||||||
|
if self.castling_rights() & (1 << 0) != 0 { fen.push('q'); }
|
||||||
|
}
|
||||||
|
|
||||||
|
fen.push(' ');
|
||||||
|
if self.en_passant_square() == 0 {
|
||||||
|
fen.push('-');
|
||||||
|
} else {
|
||||||
|
let sq = self.en_passant_square().trailing_zeros();
|
||||||
|
fen.push_str(¬ation_from_square_number(sq as u8));
|
||||||
|
}
|
||||||
|
|
||||||
|
fen.push_str(" 0 1");
|
||||||
|
|
||||||
|
return fen;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calc_occupancy(&mut self) {
|
||||||
|
self.occupancy = [0u64; 3];
|
||||||
|
for b in 0..6 {
|
||||||
|
self.occupancy[0] |= self.bitboards[b];
|
||||||
|
}
|
||||||
|
for b in 6..12 {
|
||||||
|
self.occupancy[1] |= self.bitboards[b];
|
||||||
|
}
|
||||||
|
self.occupancy[2] = self.occupancy[0] | self.occupancy[1];
|
||||||
|
}
|
||||||
|
fn calc_piece_board(&mut self) {
|
||||||
|
for sq in 0..64 {
|
||||||
|
for b in 0..12 {
|
||||||
|
if (self.bitboards[b as usize] & 1 << sq) != 0 {
|
||||||
|
self.piece_board[sq] = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn place_piece(&mut self, sq: i32, piece: char) {
|
||||||
|
match piece {
|
||||||
|
'p' => {self.bitboards[6] |= 1 << sq}
|
||||||
|
'n' => {self.bitboards[7] |= 1 << sq}
|
||||||
|
'b' => {self.bitboards[8] |= 1 << sq}
|
||||||
|
'r' => {self.bitboards[9] |= 1 << sq}
|
||||||
|
'q' => {self.bitboards[10] |= 1 << sq}
|
||||||
|
'k' => {self.bitboards[11] |= 1 << sq}
|
||||||
|
'P' => {self.bitboards[0] |= 1 << sq}
|
||||||
|
'N' => {self.bitboards[1] |= 1 << sq}
|
||||||
|
'B' => {self.bitboards[2] |= 1 << sq}
|
||||||
|
'R' => {self.bitboards[3] |= 1 << sq}
|
||||||
|
'Q' => {self.bitboards[4] |= 1 << sq}
|
||||||
|
'K' => {self.bitboards[5] |= 1 << sq}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn get_piece_character(&self, index: i32) -> Option<char> {
|
||||||
|
let sq = 1 << index;
|
||||||
|
|
||||||
|
if (self.bitboards[0] & sq) != 0 {
|
||||||
|
return Some('P');
|
||||||
|
}
|
||||||
|
if (self.bitboards[1] & sq) != 0 {
|
||||||
|
return Some('N');
|
||||||
|
}
|
||||||
|
if (self.bitboards[2] & sq) != 0 {
|
||||||
|
return Some('B');
|
||||||
|
}
|
||||||
|
if (self.bitboards[3] & sq) != 0 {
|
||||||
|
return Some('R');
|
||||||
|
}
|
||||||
|
if (self.bitboards[4] & sq) != 0 {
|
||||||
|
return Some('Q');
|
||||||
|
}
|
||||||
|
if (self.bitboards[5] & sq) != 0 {
|
||||||
|
return Some('K');
|
||||||
|
}
|
||||||
|
if (self.bitboards[6] & sq) != 0 {
|
||||||
|
return Some('p');
|
||||||
|
}
|
||||||
|
if (self.bitboards[7] & sq) != 0 {
|
||||||
|
return Some('n');
|
||||||
|
}
|
||||||
|
if (self.bitboards[8] & sq) != 0 {
|
||||||
|
return Some('b');
|
||||||
|
}
|
||||||
|
if (self.bitboards[9] & sq) != 0 {
|
||||||
|
return Some('r');
|
||||||
|
}
|
||||||
|
if (self.bitboards[10] & sq) != 0 {
|
||||||
|
return Some('q');
|
||||||
|
}
|
||||||
|
if (self.bitboards[11] & sq) != 0 {
|
||||||
|
return Some('k');
|
||||||
|
}
|
||||||
|
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
engine/src/bitboard/checkinfo.rs
Normal file
21
engine/src/bitboard/checkinfo.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
pub struct CheckInfo {
|
||||||
|
pub check_count: u8,
|
||||||
|
pub move_mask: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CheckInfo {
|
||||||
|
|
||||||
|
pub fn new() -> Self {
|
||||||
|
return Self {
|
||||||
|
check_count: 0,
|
||||||
|
move_mask: 0xFFFF_FFFF_FFFF_FFFF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn add_checker(&mut self, move_mask: u64) {
|
||||||
|
self.move_mask &= move_mask;
|
||||||
|
self.check_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
134
engine/src/bitboard/legality.rs
Normal file
134
engine/src/bitboard/legality.rs
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
use super::board::Board;
|
||||||
|
use super::attackmaps::RAY_TABLE;
|
||||||
|
use super::checkinfo::CheckInfo;
|
||||||
|
use super::attacks::get_raycast_from_square_in_direction;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
pub fn check_test(&self) -> CheckInfo {
|
||||||
|
let mut check_info: CheckInfo = CheckInfo::new();
|
||||||
|
let offset: usize = 6 * self.side_to_move as usize;
|
||||||
|
let king: u64 = self.bitboards[5 + offset];
|
||||||
|
let king_sq = king.trailing_zeros() as usize;
|
||||||
|
let occupancy = self.occupancy[2];
|
||||||
|
|
||||||
|
// queen-rook checks (+)
|
||||||
|
let attacker_mask = self.bitboards[10 - offset] | self.bitboards[9 - offset];
|
||||||
|
|
||||||
|
for dir in [0, 2, 4, 6] {
|
||||||
|
let threat_mask: u64 = get_raycast_from_square_in_direction(occupancy, king_sq, dir);
|
||||||
|
if threat_mask & attacker_mask != 0 {
|
||||||
|
check_info.add_checker(threat_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// queen-bishop checks (x)
|
||||||
|
let attacker_mask = self.bitboards[10 - offset] | self.bitboards[8 - offset];
|
||||||
|
|
||||||
|
for dir in [1, 3, 5, 7] {
|
||||||
|
let threat_mask = get_raycast_from_square_in_direction(occupancy, king_sq, dir);
|
||||||
|
if threat_mask & attacker_mask != 0 {
|
||||||
|
check_info.add_checker(threat_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// knight checks (L)
|
||||||
|
let attacker_mask = self.bitboards[7 - offset];
|
||||||
|
let threat_mask = self.get_pseudo_knight_moves(king_sq as u32);
|
||||||
|
let checker = threat_mask & attacker_mask;
|
||||||
|
if checker != 0 {
|
||||||
|
check_info.add_checker(checker);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pawn checks (v)
|
||||||
|
let attacker_mask = self.bitboards[6 - offset];
|
||||||
|
let threat_mask = self.get_pseudo_pawn_captures(king_sq as u32);
|
||||||
|
let checker = threat_mask & attacker_mask;
|
||||||
|
if checker != 0 {
|
||||||
|
check_info.add_checker(checker);
|
||||||
|
}
|
||||||
|
|
||||||
|
return check_info;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(in super) fn calc_pinned_squares(&mut self) {
|
||||||
|
self.pinned_squares = [4; 64];
|
||||||
|
self.pin_mask = 0u64;
|
||||||
|
|
||||||
|
let friendly_pieces: u64 = self.occupancy[self.side_to_move as usize];
|
||||||
|
let offset: usize = 6 * self.side_to_move as usize;
|
||||||
|
let king_board: u64 = self.bitboards[5 + offset];
|
||||||
|
let king_sq: u32 = king_board.trailing_zeros();
|
||||||
|
let opponent_queen_bishop_mask: u64 = self.bitboards[8 - offset] | self.bitboards[10 - offset];
|
||||||
|
let opponent_queen_rook_mask: u64 = self.bitboards[9 - offset] | self.bitboards[10 - offset];
|
||||||
|
|
||||||
|
// Queen-Rook directions
|
||||||
|
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_rook_mask, 0);
|
||||||
|
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_rook_mask, 2);
|
||||||
|
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_rook_mask, 4);
|
||||||
|
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_rook_mask, 6);
|
||||||
|
|
||||||
|
// Queen-Bishop directions
|
||||||
|
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_bishop_mask, 1);
|
||||||
|
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_bishop_mask, 3);
|
||||||
|
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_bishop_mask, 5);
|
||||||
|
self.set_pinned_in_ray_direction(king_sq, friendly_pieces, opponent_queen_bishop_mask, 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(in super) fn set_pinned_in_ray_direction(&mut self, king_sq: u32, friendly_pieces: u64, attackers: u64, dir: u8) {
|
||||||
|
let is_up: bool = dir / 4 == 0;
|
||||||
|
let mask: u64 = RAY_TABLE[king_sq as usize][dir as usize];
|
||||||
|
let blockers: u64 = self.occupancy[2] & mask;
|
||||||
|
if blockers == 0 { return; }
|
||||||
|
let first_blocker_sq: u32 = if is_up { blockers.trailing_zeros() } else { 63 - blockers.leading_zeros() };
|
||||||
|
if (friendly_pieces & 1 << first_blocker_sq) != 0 {
|
||||||
|
let blockers: u64 = blockers & !(1 << first_blocker_sq);
|
||||||
|
if blockers == 0 { return; }
|
||||||
|
let second_blocker_sq: u32 = if is_up { blockers.trailing_zeros() } else { 63 - blockers.leading_zeros() };
|
||||||
|
|
||||||
|
if (attackers & 1 << second_blocker_sq) != 0 {
|
||||||
|
self.pinned_squares[first_blocker_sq as usize] = dir % 4;
|
||||||
|
self.pin_mask |= 1 << first_blocker_sq;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(in super) fn get_pin_masked_moves(&self, moves: u64, sq: u32) -> u64 {
|
||||||
|
let sq: usize = sq as usize;
|
||||||
|
if self.pinned_squares[sq] == 4 { return moves; }
|
||||||
|
let dir: u8 = self.pinned_squares[sq];
|
||||||
|
return moves & (RAY_TABLE[sq][dir as usize] | RAY_TABLE[sq][4 + dir as usize]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// <----- TESTS ----->
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_test_test() {
|
||||||
|
|
||||||
|
let fens = [
|
||||||
|
"rnb1k1nr/pppppppp/4q3/8/1b6/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", // no check
|
||||||
|
"rnb1k1nr/pppppppp/4q3/8/1b1P4/8/PPP1PPPP/RNBQKBNR w KQkq d3 0 1", // single check
|
||||||
|
"rnb1k1nr/ppp1p2p/3pq1p1/8/1b1P1P2/8/PPP2PPP/RNBQKBNR w KQkq - 0 1" // double check
|
||||||
|
];
|
||||||
|
let expected_results = [
|
||||||
|
CheckInfo { check_count: 0, move_mask: 0xFFFF_FFFF_FFFF_FFFF },
|
||||||
|
CheckInfo { check_count: 1, move_mask: 0x0000_0000_0204_0800 },
|
||||||
|
CheckInfo { check_count: 2, move_mask: 0x0000_0000_0000_0000 }
|
||||||
|
];
|
||||||
|
|
||||||
|
for test_nr in 0..3 {
|
||||||
|
let fen = fens[test_nr];
|
||||||
|
let board = Board::build(fen);
|
||||||
|
let check_test_actual = board.check_test();
|
||||||
|
assert_eq!(check_test_actual.check_count, expected_results[test_nr].check_count);
|
||||||
|
assert_eq!(check_test_actual.move_mask, expected_results[test_nr].move_mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
38
engine/src/bitboard/makemove.rs
Normal file
38
engine/src/bitboard/makemove.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
mod quiets;
|
||||||
|
mod captures;
|
||||||
|
mod castles;
|
||||||
|
|
||||||
|
use super::bitmove::BitMoveType;
|
||||||
|
use super::bitmove::BitMove;
|
||||||
|
use super::board::Board;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn make_move(&mut self, played_move: &BitMove) {
|
||||||
|
let move_type = played_move.move_type();
|
||||||
|
|
||||||
|
match move_type {
|
||||||
|
BitMoveType::Quiet => {
|
||||||
|
self.make_quiet(played_move);
|
||||||
|
}
|
||||||
|
BitMoveType::Capture => {
|
||||||
|
self.make_capture(played_move);
|
||||||
|
}
|
||||||
|
BitMoveType::Castle => {
|
||||||
|
self.make_castle(played_move);
|
||||||
|
}
|
||||||
|
BitMoveType::EnPassant => {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.occupancy[2] = self.occupancy[0] | self.occupancy[1];
|
||||||
|
|
||||||
|
if self.en_passant_square != 0 {
|
||||||
|
self.en_passant_square = 0u64;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.side_to_move = 1 - self.side_to_move;
|
||||||
|
}
|
||||||
|
}
|
||||||
82
engine/src/bitboard/makemove/captures.rs
Normal file
82
engine/src/bitboard/makemove/captures.rs
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
pub fn make_capture(&mut self, played_move: &BitMove) {
|
||||||
|
let main_from: usize = played_move.from_square() as usize;
|
||||||
|
let main_to: usize = played_move.to_square() as usize;
|
||||||
|
let main_piece: usize = self.piece_board(main_from as u8) as usize;
|
||||||
|
let friendly_occupancy = main_piece/6;
|
||||||
|
|
||||||
|
let color_offset = self.side_to_move * 6;
|
||||||
|
let castling_offset = 2 - 2 * self.side_to_move as usize;
|
||||||
|
let castling_rights = self.castling_rights >> castling_offset;
|
||||||
|
|
||||||
|
let mut taken_piece = 0u8;
|
||||||
|
|
||||||
|
taken_piece = self.piece_board(main_to as u8);
|
||||||
|
let secondary_piece = taken_piece as usize;
|
||||||
|
let secondary_from = main_to;
|
||||||
|
|
||||||
|
let opponent_castling_offset = 2 * self.side_to_move as usize;
|
||||||
|
let opponent_castling_rights = self.castling_rights >> opponent_castling_offset;
|
||||||
|
|
||||||
|
let opponent_occupancy = 1 - self.side_to_move as usize;
|
||||||
|
|
||||||
|
self.bitboards[main_piece] &= !(1 << main_from);
|
||||||
|
self.occupancy[friendly_occupancy] &= !(1 << main_from);
|
||||||
|
self.piece_board[main_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
self.bitboards[secondary_piece] &= !(1 << secondary_from);
|
||||||
|
self.occupancy[opponent_occupancy] &= !(1 << secondary_from);
|
||||||
|
self.piece_board[secondary_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
if opponent_castling_rights != 0
|
||||||
|
&& secondary_piece == 9 - color_offset as usize{
|
||||||
|
|
||||||
|
let back_rank_offset = 56 - 56 * self.side_to_move as usize;
|
||||||
|
if opponent_castling_rights & 0b01 != 0
|
||||||
|
&& secondary_from == back_rank_offset {
|
||||||
|
self.castling_rights &= !(1 << opponent_castling_offset);
|
||||||
|
}
|
||||||
|
else if opponent_castling_rights & 0b10 != 0
|
||||||
|
&& secondary_from == 7 + back_rank_offset {
|
||||||
|
self.castling_rights &= !(2 << opponent_castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(promotion_piece) = played_move.promotion_piece() {
|
||||||
|
let promotion_piece = (color_offset + promotion_piece) as usize;
|
||||||
|
self.bitboards[promotion_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = promotion_piece as u8;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self.bitboards[main_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = main_piece as u8;
|
||||||
|
|
||||||
|
if main_piece == 5 + color_offset as usize
|
||||||
|
&& castling_rights != 0 {
|
||||||
|
if castling_rights & 0b1 != 0 {
|
||||||
|
self.castling_rights &= !(1 << castling_offset);
|
||||||
|
}
|
||||||
|
if castling_rights & 0b10 != 0 {
|
||||||
|
self.castling_rights &= !(2 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if main_piece == 3 + color_offset as usize
|
||||||
|
&& castling_rights != 0 {
|
||||||
|
let back_rank_offset = 56 * self.side_to_move as usize;
|
||||||
|
|
||||||
|
if castling_rights & 0b10 != 0
|
||||||
|
&& main_from == 7 + back_rank_offset {
|
||||||
|
self.castling_rights &= !(2 << castling_offset);
|
||||||
|
}
|
||||||
|
else if castling_rights & 0b1 != 0
|
||||||
|
&& main_from == back_rank_offset {
|
||||||
|
self.castling_rights &= !(1 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
engine/src/bitboard/makemove/castles.rs
Normal file
35
engine/src/bitboard/makemove/castles.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
pub fn make_castle(&mut self, played_move: &BitMove) {
|
||||||
|
let main_from: usize = played_move.from_square() as usize;
|
||||||
|
let main_to: usize = played_move.to_square() as usize;
|
||||||
|
let main_piece: usize = self.piece_board(main_from as u8) as usize;
|
||||||
|
let friendly_occupancy = main_piece/6;
|
||||||
|
|
||||||
|
let castling_offset = 2 - 2 * self.side_to_move as usize;
|
||||||
|
|
||||||
|
let secondary_piece: usize = main_piece - 2;
|
||||||
|
let is_kingside = main_to%8 > 4;
|
||||||
|
let secondary_from: usize = if is_kingside { main_to + 1 } else { main_to - 2 };
|
||||||
|
let secondary_to: usize = if is_kingside { main_to - 1 } else { main_to + 1 };
|
||||||
|
|
||||||
|
self.bitboards[main_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = main_piece as u8;
|
||||||
|
|
||||||
|
self.bitboards[main_piece] &= !(1 << main_from);
|
||||||
|
self.occupancy[friendly_occupancy] &= !(1 << main_from);
|
||||||
|
self.piece_board[main_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
self.bitboards[secondary_piece] |= 1 << secondary_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << secondary_to;
|
||||||
|
self.piece_board[secondary_to] = secondary_piece as u8;
|
||||||
|
|
||||||
|
self.bitboards[secondary_piece] &= !(1 << secondary_from);
|
||||||
|
self.occupancy[friendly_occupancy] &= !(1 << secondary_from);
|
||||||
|
self.piece_board[secondary_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
self.castling_rights &= !(3 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
61
engine/src/bitboard/makemove/quiets.rs
Normal file
61
engine/src/bitboard/makemove/quiets.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
pub fn make_quiet(&mut self, played_move: &BitMove) {
|
||||||
|
let main_from: usize = played_move.from_square() as usize;
|
||||||
|
let main_to: usize = played_move.to_square() as usize;
|
||||||
|
let main_piece: usize = self.piece_board(main_from as u8) as usize;
|
||||||
|
let friendly_occupancy = main_piece/6;
|
||||||
|
|
||||||
|
let color_offset = self.side_to_move * 6;
|
||||||
|
let castling_offset = 2 - 2 * self.side_to_move as usize;
|
||||||
|
let castling_rights = self.castling_rights >> castling_offset;
|
||||||
|
|
||||||
|
self.bitboards[main_piece] &= !(1 << main_from);
|
||||||
|
self.occupancy[friendly_occupancy] &= !(1 << main_from);
|
||||||
|
self.piece_board[main_from] = Self::EMPTY_SQUARE;
|
||||||
|
|
||||||
|
if let Some(promotion_piece) = played_move.promotion_piece() {
|
||||||
|
let promotion_piece = (color_offset + promotion_piece) as usize;
|
||||||
|
self.bitboards[promotion_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = promotion_piece as u8;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self.bitboards[main_piece] |= 1 << main_to;
|
||||||
|
self.occupancy[friendly_occupancy] |= 1 << main_to;
|
||||||
|
self.piece_board[main_to] = main_piece as u8;
|
||||||
|
|
||||||
|
if main_piece == 0 && (main_to - main_from) == 16 {
|
||||||
|
let new_en_passant = main_to - 8;
|
||||||
|
self.en_passant_square = 1 << new_en_passant;
|
||||||
|
}
|
||||||
|
else if main_piece == 6 && (main_from - main_to) == 16 {
|
||||||
|
let new_en_passant = main_to + 8;
|
||||||
|
self.en_passant_square = 1 << new_en_passant;
|
||||||
|
}
|
||||||
|
else if main_piece == 5 + color_offset as usize
|
||||||
|
&& castling_rights != 0 {
|
||||||
|
if castling_rights & 0b1 != 0 {
|
||||||
|
self.castling_rights &= !(1 << castling_offset);
|
||||||
|
}
|
||||||
|
if castling_rights & 0b10 != 0 {
|
||||||
|
self.castling_rights &= !(2 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if main_piece == 3 + color_offset as usize
|
||||||
|
&& castling_rights != 0 {
|
||||||
|
let back_rank_offset = 56 * self.side_to_move as usize;
|
||||||
|
|
||||||
|
if castling_rights & 0b10 != 0
|
||||||
|
&& main_from == 7 + back_rank_offset {
|
||||||
|
self.castling_rights &= !(2 << castling_offset);
|
||||||
|
}
|
||||||
|
else if castling_rights & 0b1 != 0
|
||||||
|
&& main_from == back_rank_offset {
|
||||||
|
self.castling_rights &= !(1 << castling_offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
engine/src/bitboard/movebuffer.rs
Normal file
44
engine/src/bitboard/movebuffer.rs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
use super::bitmove::BitMove;
|
||||||
|
|
||||||
|
pub struct MoveBuffer {
|
||||||
|
|
||||||
|
buffer: [BitMove; 256],
|
||||||
|
count: usize
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MoveBuffer {
|
||||||
|
|
||||||
|
pub fn new() -> Self {
|
||||||
|
return Self {
|
||||||
|
buffer: [BitMove::quiet(0, 0, None); 256],
|
||||||
|
count: 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn add(&mut self, bitmove: BitMove) {
|
||||||
|
self.buffer[self.count] = bitmove;
|
||||||
|
self.count += 1;
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn append(&mut self, other: &MoveBuffer) {
|
||||||
|
self.buffer[self.count..self.count + other.count()].copy_from_slice(other.contents());
|
||||||
|
self.count += other.count();
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.count = 0;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn count(&self) -> usize{
|
||||||
|
return self.count;
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn get(&self, idx: usize) -> &BitMove {
|
||||||
|
return &self.buffer[idx];
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn contents(&self) -> &[BitMove] {
|
||||||
|
return &self.buffer[0..self.count];
|
||||||
|
}
|
||||||
|
}
|
||||||
67
engine/src/bitboard/movegen.rs
Normal file
67
engine/src/bitboard/movegen.rs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
mod pawns;
|
||||||
|
mod knights;
|
||||||
|
mod bishops;
|
||||||
|
mod rooks;
|
||||||
|
mod queens;
|
||||||
|
mod kings;
|
||||||
|
|
||||||
|
use super::board::Board;
|
||||||
|
use super::movebuffer::MoveBuffer;
|
||||||
|
use super::bitmove::BitMove;
|
||||||
|
use super::checkinfo::CheckInfo;
|
||||||
|
use super::utils::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
const NO_FILTER: u64 = 0xFFFF_FFFF_FFFF_FFFF;
|
||||||
|
|
||||||
|
pub fn collect_moves(&mut self, buffer: &mut MoveBuffer, temp_buffer: &mut MoveBuffer) -> bool {
|
||||||
|
buffer.clear();
|
||||||
|
self.calc_pinned_squares();
|
||||||
|
let check_info = self.check_test();
|
||||||
|
|
||||||
|
match check_info.check_count {
|
||||||
|
0 => self.collect_all_moves(buffer, temp_buffer),
|
||||||
|
1 => self.collect_moves_single_check(buffer, temp_buffer, &check_info),
|
||||||
|
2 => self.collect_king_evasion(buffer, temp_buffer),
|
||||||
|
_ => panic!("More than 2 checking pieces found as the same time!")
|
||||||
|
}
|
||||||
|
return check_info.check_count > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(in super) fn collect_all_moves(&self, buffer: &mut MoveBuffer, temp_buffer: &mut MoveBuffer) {
|
||||||
|
let safe_squares = self.get_safe_king_squares();
|
||||||
|
|
||||||
|
self.add_pawn_moves(buffer, temp_buffer, Self::NO_FILTER);
|
||||||
|
self.add_knight_moves(buffer, temp_buffer, Self::NO_FILTER);
|
||||||
|
self.add_bishop_moves(buffer, temp_buffer, Self::NO_FILTER);
|
||||||
|
self.add_rook_moves(buffer, temp_buffer, Self::NO_FILTER);
|
||||||
|
self.add_queen_moves(buffer, temp_buffer, Self::NO_FILTER);
|
||||||
|
self.add_king_moves(buffer, temp_buffer, safe_squares);
|
||||||
|
self.add_king_castles(buffer, safe_squares);
|
||||||
|
|
||||||
|
buffer.append(temp_buffer);
|
||||||
|
temp_buffer.clear();
|
||||||
|
}
|
||||||
|
pub(in super) fn collect_moves_single_check(&self, buffer: &mut MoveBuffer, temp_buffer: &mut MoveBuffer, check_info: &CheckInfo) {
|
||||||
|
let safe_squares = self.get_safe_king_squares();
|
||||||
|
|
||||||
|
self.add_pawn_moves(buffer, temp_buffer, check_info.move_mask);
|
||||||
|
self.add_knight_moves(buffer, temp_buffer, check_info.move_mask);
|
||||||
|
self.add_bishop_moves(buffer, temp_buffer, check_info.move_mask);
|
||||||
|
self.add_rook_moves(buffer, temp_buffer, check_info.move_mask);
|
||||||
|
self.add_queen_moves(buffer, temp_buffer, check_info.move_mask);
|
||||||
|
self.add_king_moves(buffer, temp_buffer, safe_squares);
|
||||||
|
|
||||||
|
buffer.append(temp_buffer);
|
||||||
|
temp_buffer.clear();
|
||||||
|
}
|
||||||
|
pub(in super) fn collect_king_evasion(&self, buffer: &mut MoveBuffer, temp_buffer: &mut MoveBuffer) {
|
||||||
|
let safe_squares = self.get_safe_king_squares();
|
||||||
|
|
||||||
|
self.add_king_moves(buffer, temp_buffer, safe_squares);
|
||||||
|
|
||||||
|
buffer.append(&temp_buffer);
|
||||||
|
temp_buffer.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
55
engine/src/bitboard/movegen/bishops.rs
Normal file
55
engine/src/bitboard/movegen/bishops.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
pub fn add_bishop_moves(&self, capture_buffer: &mut MoveBuffer, quiet_buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let piece_index = 2 + self.side_to_move * 6;
|
||||||
|
let mut bishops = self.bitboards[piece_index as usize];
|
||||||
|
let empty = !self.occupancy[2];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while bishops != 0 {
|
||||||
|
let from_sq = pop_lsb(&mut bishops);
|
||||||
|
let raw_move_map = self.get_pseudo_bishop_moves(from_sq) & move_mask;
|
||||||
|
let move_map = self.get_pin_masked_moves(raw_move_map, from_sq);
|
||||||
|
|
||||||
|
let mut quiet_map = move_map & empty;
|
||||||
|
let mut capture_map = move_map & opponents;
|
||||||
|
|
||||||
|
while quiet_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut quiet_map);
|
||||||
|
quiet_buffer.add(BitMove::quiet(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
while capture_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut capture_map);
|
||||||
|
capture_buffer.add(BitMove::capture(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_bishop_captures(&self, buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let offset = 6 * self.side_to_move as usize;
|
||||||
|
let mut bishops: u64 = self.bitboards[2 + offset];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while bishops != 0 {
|
||||||
|
let next_sq: u32 = pop_lsb(&mut bishops);
|
||||||
|
let mut attacks: u64 = self.get_pseudo_bishop_moves(next_sq) & opponents & move_mask;
|
||||||
|
attacks = self.get_pin_masked_moves(attacks, next_sq);
|
||||||
|
|
||||||
|
while attacks != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut attacks);
|
||||||
|
buffer.add(BitMove::capture(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
88
engine/src/bitboard/movegen/kings.rs
Normal file
88
engine/src/bitboard/movegen/kings.rs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
pub fn add_king_moves(&self, capture_buffer: &mut MoveBuffer, quiet_buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let piece_index = 5 + self.side_to_move * 6;
|
||||||
|
let mut kings = self.bitboards[piece_index as usize];
|
||||||
|
let empty = !self.occupancy[2];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while kings != 0 {
|
||||||
|
let from_sq = pop_lsb(&mut kings);
|
||||||
|
let move_map = self.get_pseudo_king_moves(from_sq) & move_mask;
|
||||||
|
|
||||||
|
let mut quiet_map = move_map & empty;
|
||||||
|
let mut capture_map = move_map & opponents;
|
||||||
|
|
||||||
|
while quiet_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut quiet_map);
|
||||||
|
quiet_buffer.add(BitMove::quiet(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
while capture_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut capture_map);
|
||||||
|
capture_buffer.add(BitMove::capture(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_king_captures(&self, buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let offset = 6 * self.side_to_move as usize;
|
||||||
|
let mut kings: u64 = self.bitboards[5 + offset];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while kings != 0 {
|
||||||
|
let next_sq: u32 = pop_lsb(&mut kings);
|
||||||
|
let mut attacks: u64 = self.get_pseudo_king_moves(next_sq) & opponents & move_mask;
|
||||||
|
attacks = self.get_pin_masked_moves(attacks, next_sq);
|
||||||
|
|
||||||
|
while attacks != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut attacks);
|
||||||
|
buffer.add(BitMove::capture(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_king_castles(&self, buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
if self.castling_rights & (0b11 << (2 - 2 * self.side_to_move)) == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let offset = 5 + 6 * self.side_to_move as u8;
|
||||||
|
let castle_offset = 2 - 2 * self.side_to_move as u8;
|
||||||
|
let castling_rights = self.castling_rights & 3 << castle_offset;
|
||||||
|
let occupied = self.occupancy[2];
|
||||||
|
let king_sq = self.bitboards[offset as usize].trailing_zeros();
|
||||||
|
|
||||||
|
let queenside_mask = 0b111 << (king_sq - 3);
|
||||||
|
let kingside_mask = 0b11 << (king_sq + 1);
|
||||||
|
|
||||||
|
if (castling_rights & 1 << castle_offset) != 0
|
||||||
|
&& queenside_mask & occupied == 0
|
||||||
|
&& !move_mask & 0b11 << (king_sq - 2) == 0
|
||||||
|
&& !self.is_square_attacked(king_sq - 2) {
|
||||||
|
buffer.add(BitMove::castle(
|
||||||
|
king_sq as u8,
|
||||||
|
(king_sq - 2) as u8
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if (castling_rights & 2 << castle_offset) != 0
|
||||||
|
&& kingside_mask & occupied == 0
|
||||||
|
&& !move_mask & 0b11 << (king_sq + 1) == 0
|
||||||
|
&& !self.is_square_attacked(king_sq + 2) {
|
||||||
|
buffer.add(BitMove::castle(
|
||||||
|
king_sq as u8,
|
||||||
|
(king_sq + 2) as u8
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
55
engine/src/bitboard/movegen/knights.rs
Normal file
55
engine/src/bitboard/movegen/knights.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
pub fn add_knight_moves(&self, capture_buffer: &mut MoveBuffer, quiet_buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let piece_index = 1 + self.side_to_move * 6;
|
||||||
|
let mut knights = self.bitboards[piece_index as usize];
|
||||||
|
let empty = !self.occupancy[2];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while knights != 0 {
|
||||||
|
let from_sq = pop_lsb(&mut knights);
|
||||||
|
let raw_move_map = self.get_pseudo_knight_moves(from_sq) & move_mask;
|
||||||
|
let move_map = self.get_pin_masked_moves(raw_move_map, from_sq);
|
||||||
|
|
||||||
|
let mut quiet_map = move_map & empty;
|
||||||
|
let mut capture_map = move_map & opponents;
|
||||||
|
|
||||||
|
while quiet_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut quiet_map);
|
||||||
|
quiet_buffer.add(BitMove::quiet(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
while capture_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut capture_map);
|
||||||
|
capture_buffer.add(BitMove::capture(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_knight_captures(&self, buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let offset = 6 * self.side_to_move as usize;
|
||||||
|
let mut knights: u64 = self.bitboards[1 + offset];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while knights != 0 {
|
||||||
|
let next_sq: u32 = pop_lsb(&mut knights);
|
||||||
|
let mut attacks: u64 = self.get_pseudo_knight_moves(next_sq) & opponents & move_mask;
|
||||||
|
attacks = self.get_pin_masked_moves(attacks, next_sq);
|
||||||
|
|
||||||
|
while attacks != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut attacks);
|
||||||
|
buffer.add(BitMove::capture(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
76
engine/src/bitboard/movegen/pawns.rs
Normal file
76
engine/src/bitboard/movegen/pawns.rs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
pub fn add_pawn_quiets(&self, buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let offset: u8 = self.side_to_move * 6;
|
||||||
|
let mut pawns: u64 = self.bitboards[offset as usize];
|
||||||
|
while pawns != 0 {
|
||||||
|
let next_sq = pop_lsb(&mut pawns);
|
||||||
|
|
||||||
|
let mut quiets: u64 = self.get_pseudo_pawn_moves(next_sq) & move_mask;
|
||||||
|
quiets = self.get_pin_masked_moves(quiets, next_sq);
|
||||||
|
while quiets != 0 {
|
||||||
|
let to_sq = quiets.trailing_zeros();
|
||||||
|
|
||||||
|
if (self.side_to_move == 0 && quiets.trailing_zeros() / 8 == 7)
|
||||||
|
|| (self.side_to_move == 1 && quiets.trailing_zeros() / 8 == 0) {
|
||||||
|
for piece_type in [4, 3, 2, 1] {
|
||||||
|
buffer.add(BitMove::quiet(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
Some(piece_type)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
buffer.add(BitMove::quiet(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
quiets &= !(1 << to_sq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_pawn_captures(&self, buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let offset = 6 * self.side_to_move as usize;
|
||||||
|
let mut pawns: u64 = self.bitboards[offset];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while pawns != 0 {
|
||||||
|
let next_sq = pop_lsb(&mut pawns);
|
||||||
|
|
||||||
|
let mut attacks: u64 = self.get_pseudo_pawn_captures(next_sq) & move_mask;
|
||||||
|
attacks = self.get_pin_masked_moves(attacks, next_sq);
|
||||||
|
attacks &= opponents;
|
||||||
|
|
||||||
|
while attacks != 0 {
|
||||||
|
let to_sq = attacks.trailing_zeros();
|
||||||
|
|
||||||
|
if (self.side_to_move == 0 && attacks.trailing_zeros() / 8 == 7)
|
||||||
|
|| (self.side_to_move == 1 && attacks.trailing_zeros() / 8 == 0) {
|
||||||
|
for piece_type in [4, 3, 2, 1] {
|
||||||
|
buffer.add(BitMove::capture(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
Some(piece_type)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
buffer.add(BitMove::capture(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
attacks &= !(1 << to_sq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_pawn_moves(&self, capture_buffer: &mut MoveBuffer, quiet_buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
self.add_pawn_captures(capture_buffer, move_mask);
|
||||||
|
self.add_pawn_quiets(quiet_buffer, move_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
55
engine/src/bitboard/movegen/queens.rs
Normal file
55
engine/src/bitboard/movegen/queens.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
pub fn add_queen_moves(&self, capture_buffer: &mut MoveBuffer, quiet_buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let piece_index = 4 + self.side_to_move * 6;
|
||||||
|
let mut queens = self.bitboards[piece_index as usize];
|
||||||
|
let empty = !self.occupancy[2];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while queens != 0 {
|
||||||
|
let from_sq = pop_lsb(&mut queens);
|
||||||
|
let raw_move_map = self.get_pseudo_queen_moves(from_sq) & move_mask;
|
||||||
|
let move_map = self.get_pin_masked_moves(raw_move_map, from_sq);
|
||||||
|
|
||||||
|
let mut quiet_map = move_map & empty;
|
||||||
|
let mut capture_map = move_map & opponents;
|
||||||
|
|
||||||
|
while quiet_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut quiet_map);
|
||||||
|
quiet_buffer.add(BitMove::quiet(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
while capture_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut capture_map);
|
||||||
|
capture_buffer.add(BitMove::capture(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_queen_captures(&self, buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let offset = 6 * self.side_to_move as usize;
|
||||||
|
let mut queens: u64 = self.bitboards[4 + offset];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while queens != 0 {
|
||||||
|
let next_sq: u32 = pop_lsb(&mut queens);
|
||||||
|
let mut attacks: u64 = self.get_pseudo_queen_moves(next_sq) & opponents & move_mask;
|
||||||
|
attacks = self.get_pin_masked_moves(attacks, next_sq);
|
||||||
|
|
||||||
|
while attacks != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut attacks);
|
||||||
|
buffer.add(BitMove::capture(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
55
engine/src/bitboard/movegen/rooks.rs
Normal file
55
engine/src/bitboard/movegen/rooks.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Board {
|
||||||
|
|
||||||
|
pub fn add_rook_moves(&self, capture_buffer: &mut MoveBuffer, quiet_buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let piece_index = 3 + self.side_to_move * 6;
|
||||||
|
let mut rooks = self.bitboards[piece_index as usize];
|
||||||
|
let empty = !self.occupancy[2];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while rooks != 0 {
|
||||||
|
let from_sq = pop_lsb(&mut rooks);
|
||||||
|
let raw_move_map = self.get_pseudo_rook_moves(from_sq) & move_mask;
|
||||||
|
let move_map = self.get_pin_masked_moves(raw_move_map, from_sq);
|
||||||
|
|
||||||
|
let mut quiet_map = move_map & empty;
|
||||||
|
let mut capture_map = move_map & opponents;
|
||||||
|
|
||||||
|
while quiet_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut quiet_map);
|
||||||
|
quiet_buffer.add(BitMove::quiet(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
while capture_map != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut capture_map);
|
||||||
|
capture_buffer.add(BitMove::capture(
|
||||||
|
from_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_rook_captures(&self, buffer: &mut MoveBuffer, move_mask: u64) {
|
||||||
|
let offset = 6 * self.side_to_move as usize;
|
||||||
|
let mut rooks: u64 = self.bitboards[3 + offset];
|
||||||
|
let opponents = self.occupancy[1 - self.side_to_move as usize];
|
||||||
|
while rooks != 0 {
|
||||||
|
let next_sq: u32 = pop_lsb(&mut rooks);
|
||||||
|
let mut attacks: u64 = self.get_pseudo_rook_moves(next_sq) & opponents & move_mask;
|
||||||
|
attacks = self.get_pin_masked_moves(attacks, next_sq);
|
||||||
|
|
||||||
|
while attacks != 0 {
|
||||||
|
let to_sq = pop_lsb(&mut attacks);
|
||||||
|
buffer.add(BitMove::capture(
|
||||||
|
next_sq as u8,
|
||||||
|
to_sq as u8,
|
||||||
|
None
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
125
engine/src/bitboard/utils.rs
Normal file
125
engine/src/bitboard/utils.rs
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
#[inline(always)]
|
||||||
|
pub fn pop_lsb(value: &mut u64) -> u32 {
|
||||||
|
let idx = value.trailing_zeros();
|
||||||
|
*value &= !(1 << idx);
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn pop_msb(value: &mut u64) -> u32 {
|
||||||
|
let idx = 63 - value.leading_zeros();
|
||||||
|
*value &= !(1 << idx);
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
const RANK_NUMBERS: [char; 8] = ['1', '2', '3', '4', '5', '6', '7', '8'];
|
||||||
|
const FILE_LETTERS: [char; 8] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
|
||||||
|
pub fn notation_from_square_number(sq: u8) -> String {
|
||||||
|
let row = sq / 8;
|
||||||
|
let col = sq % 8;
|
||||||
|
let mut notation = String::new();
|
||||||
|
|
||||||
|
let row_not = RANK_NUMBERS[row as usize];
|
||||||
|
let col_not = FILE_LETTERS[col as usize];
|
||||||
|
|
||||||
|
notation.push(col_not);
|
||||||
|
notation.push(row_not);
|
||||||
|
return notation;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn try_get_square_number_from_notation(notation: &str) -> Result<u8, ()> {
|
||||||
|
|
||||||
|
let file = match notation.chars().nth(0).unwrap() {
|
||||||
|
'a' => 0,
|
||||||
|
'b' => 1,
|
||||||
|
'c' => 2,
|
||||||
|
'd' => 3,
|
||||||
|
'e' => 4,
|
||||||
|
'f' => 5,
|
||||||
|
'g' => 6,
|
||||||
|
'h' => 7,
|
||||||
|
_ => { return Result::Err(()); }
|
||||||
|
};
|
||||||
|
if let Some(rank) = notation.chars().nth(1) {
|
||||||
|
return Result::Ok(file + 8 * (rank.to_digit(10).unwrap() as u8) - 8);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Result::Err(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const PIECE_CHARACTERS: [char; 12] = ['P', 'N', 'B', 'R', 'Q', 'K', 'p', 'n', 'b', 'r', 'q', 'k'];
|
||||||
|
pub fn get_character_by_piece_id(id: u8) -> char {
|
||||||
|
return PIECE_CHARACTERS[id as usize];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// <----- TESTS ----->
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pop_lsb_test() {
|
||||||
|
|
||||||
|
// test setup
|
||||||
|
let test_values: [u64; 6] = [
|
||||||
|
0x8000_0000_0000_0000,
|
||||||
|
0x4E91_CF05_713E_451B,
|
||||||
|
0xD588_2D58_6962_34B0,
|
||||||
|
0x9581_3335_DCAB_1DD4,
|
||||||
|
0xBEAC_DBE0_903A_AC00,
|
||||||
|
0x01E8_C895_A6F0_0000
|
||||||
|
];
|
||||||
|
let expected_values: [u32; 6] = [63, 0, 4, 2, 10, 20];
|
||||||
|
|
||||||
|
// tests
|
||||||
|
for index in 0..6 {
|
||||||
|
let mut test_value = test_values[index];
|
||||||
|
assert_eq!(pop_lsb(&mut test_value), expected_values[index])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pop_msb_test() {
|
||||||
|
// test setup
|
||||||
|
let test_values: [u64; 6] = [
|
||||||
|
0x86D6_8EB0_96A8_8D1C,
|
||||||
|
0x0000_0000_0000_0001,
|
||||||
|
0x3809_24AF_A7AE_8129,
|
||||||
|
0x0277_DA36_3B31_86D9,
|
||||||
|
0x0000_C1C3_201C_0DB1,
|
||||||
|
0x0000_0203_0DE4_E944
|
||||||
|
];
|
||||||
|
let expected_values: [u32; 6] = [63, 0, 61, 57, 47, 41];
|
||||||
|
|
||||||
|
// tests
|
||||||
|
for index in 0..6 {
|
||||||
|
let mut test_value = test_values[index];
|
||||||
|
assert_eq!(pop_msb(&mut test_value), expected_values[index])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn notation_from_square_number_test() {
|
||||||
|
// test setup
|
||||||
|
let square_indices: [u8; 8] = [1, 12, 22, 27, 32, 47, 53, 58];
|
||||||
|
let notations: [String; 8] = [
|
||||||
|
String::from("b1"),
|
||||||
|
String::from("e2"),
|
||||||
|
String::from("g3"),
|
||||||
|
String::from("d4"),
|
||||||
|
String::from("a5"),
|
||||||
|
String::from("h6"),
|
||||||
|
String::from("f7"),
|
||||||
|
String::from("c8")
|
||||||
|
];
|
||||||
|
|
||||||
|
// tests
|
||||||
|
for index in 0..8 {
|
||||||
|
let notation = notation_from_square_number(square_indices[index].clone());
|
||||||
|
assert_eq!(notation, notations[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
engine/src/boardsquare.rs
Normal file
48
engine/src/boardsquare.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct BoardSquare {
|
||||||
|
pub x: usize,
|
||||||
|
pub y: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BoardSquare {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
return Self { x: 0, y: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_coord(x: usize, y: usize) -> Self {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
if x > 7 {
|
||||||
|
println!(
|
||||||
|
"Warning: x coordinate of square is bigger than 7, it might not be on the board!"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if y > 7 {
|
||||||
|
println!(
|
||||||
|
"Warning: y coordinate of square is bigger than 7, it might not be on the board!"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Self { x: x, y: y };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(in super) fn from_index(idx: u8) -> Self {
|
||||||
|
let file = idx % 8;
|
||||||
|
let rank = idx / 8;
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
if !(0..8).contains(&rank) {
|
||||||
|
println!("Warning: internal engine issue, given index is not on the board!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Self {x: file as usize, y: rank as usize};
|
||||||
|
}
|
||||||
|
pub(in super) fn to_index(&self) -> u8 {
|
||||||
|
return (8 * self.y + self.x) as u8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
188
engine/src/chessmove.rs
Normal file
188
engine/src/chessmove.rs
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
use crate::{bitboard::{bitmove::{BitMove, BitMoveType}, board::Board}};
|
||||||
|
|
||||||
|
use super::boardsquare::BoardSquare;
|
||||||
|
use super::piecetype::PieceType;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
/*pub struct ChessMove {
|
||||||
|
pub move_type: MoveType,
|
||||||
|
pub piece_type: PieceType,
|
||||||
|
pub from_square: BoardSquare,
|
||||||
|
pub to_square: BoardSquare,
|
||||||
|
pub rook_from: BoardSquare,
|
||||||
|
pub rook_to: BoardSquare,
|
||||||
|
pub promotion_piece: Option<PieceType>,
|
||||||
|
}*/
|
||||||
|
pub enum ChessMove {
|
||||||
|
Quiet {
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
promotion_piece: Option<PieceType>
|
||||||
|
},
|
||||||
|
Capture {
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
captured_piece: PieceType,
|
||||||
|
promotion_piece: Option<PieceType>
|
||||||
|
},
|
||||||
|
Castle {
|
||||||
|
king_type: PieceType,
|
||||||
|
king_from: BoardSquare,
|
||||||
|
king_to: BoardSquare,
|
||||||
|
rook_type: PieceType,
|
||||||
|
rook_from: BoardSquare,
|
||||||
|
rook_to: BoardSquare
|
||||||
|
},
|
||||||
|
EnPassant {
|
||||||
|
pawn_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
captured_piece: PieceType,
|
||||||
|
captured_from: BoardSquare
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChessMove {
|
||||||
|
pub fn quiet(
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
promotion_piece: Option<PieceType>,
|
||||||
|
) -> Self {
|
||||||
|
return Self::Quiet {
|
||||||
|
piece_type,
|
||||||
|
from_square,
|
||||||
|
to_square,
|
||||||
|
promotion_piece
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn capture(
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
captured_piece: PieceType,
|
||||||
|
promotion_piece: Option<PieceType>,
|
||||||
|
) -> Self {
|
||||||
|
return Self::Capture {
|
||||||
|
piece_type,
|
||||||
|
from_square,
|
||||||
|
to_square,
|
||||||
|
captured_piece,
|
||||||
|
promotion_piece
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn castle(
|
||||||
|
king_type: PieceType,
|
||||||
|
king_from: BoardSquare,
|
||||||
|
king_to: BoardSquare,
|
||||||
|
rook_type: PieceType,
|
||||||
|
rook_from: BoardSquare,
|
||||||
|
rook_to: BoardSquare,
|
||||||
|
) -> Self {
|
||||||
|
return Self::Castle {
|
||||||
|
king_type,
|
||||||
|
king_from,
|
||||||
|
king_to,
|
||||||
|
rook_type,
|
||||||
|
rook_from,
|
||||||
|
rook_to
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(in super) fn from_bitmove(bitmove: &BitMove, board: &Board) -> Self {
|
||||||
|
match bitmove.move_type() {
|
||||||
|
BitMoveType::Quiet => {
|
||||||
|
let from_square_index = bitmove.from_square();
|
||||||
|
let piece_type = PieceType::from_index(board.piece_board(from_square_index));
|
||||||
|
let from_square = BoardSquare::from_index(from_square_index);
|
||||||
|
let to_square = BoardSquare::from_index(bitmove.to_square());
|
||||||
|
let promotion_piece = match bitmove.promotion_piece() {
|
||||||
|
Some(piece) => Some(PieceType::from_index(piece)),
|
||||||
|
None => None
|
||||||
|
};
|
||||||
|
|
||||||
|
return ChessMove::Quiet { piece_type, from_square, to_square, promotion_piece }
|
||||||
|
},
|
||||||
|
BitMoveType::Capture => {
|
||||||
|
let from_square_index = bitmove.from_square();
|
||||||
|
let to_square_index = bitmove.to_square();
|
||||||
|
let piece_type = PieceType::from_index(board.piece_board(from_square_index));
|
||||||
|
let from_square = BoardSquare::from_index(from_square_index);
|
||||||
|
let to_square = BoardSquare::from_index(to_square_index);
|
||||||
|
let captured_piece = PieceType::from_index(board.piece_board(to_square_index));
|
||||||
|
let promotion_piece = match bitmove.promotion_piece() {
|
||||||
|
Some(piece) => Some(PieceType::from_index(piece)),
|
||||||
|
None => None
|
||||||
|
};
|
||||||
|
|
||||||
|
return ChessMove::Capture { piece_type, from_square, to_square, captured_piece, promotion_piece }
|
||||||
|
},
|
||||||
|
BitMoveType::Castle => {
|
||||||
|
let from_square_index = bitmove.from_square();
|
||||||
|
let to_square_index = bitmove.to_square();
|
||||||
|
let king_type = PieceType::from_index(board.piece_board(from_square_index));
|
||||||
|
let king_from = BoardSquare::from_index(from_square_index);
|
||||||
|
let king_to = BoardSquare::from_index(to_square_index);
|
||||||
|
let rook_type = if bitmove.from_square() < 32 { PieceType::WhiteRook } else { PieceType::BlackRook };
|
||||||
|
let rook_from_index = if bitmove.to_square() > bitmove.from_square() {
|
||||||
|
bitmove.from_square() + 3
|
||||||
|
} else {
|
||||||
|
bitmove.from_square() - 4
|
||||||
|
};
|
||||||
|
let rook_from = BoardSquare::from_index(rook_from_index);
|
||||||
|
let rook_to_index = if bitmove.to_square() > bitmove.from_square() {
|
||||||
|
bitmove.from_square() + 1
|
||||||
|
} else {
|
||||||
|
bitmove.from_square() - 1
|
||||||
|
};
|
||||||
|
let rook_to = BoardSquare::from_index(rook_to_index);
|
||||||
|
|
||||||
|
return ChessMove::Castle { king_type, king_from, king_to, rook_type, rook_from, rook_to }
|
||||||
|
},
|
||||||
|
BitMoveType::EnPassant => {
|
||||||
|
panic!("ChessMove::from_bitmove was left unimplemented");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(in super) fn to_bitmove(&self) -> BitMove {
|
||||||
|
let bitmove = match self {
|
||||||
|
ChessMove::Quiet { piece_type, from_square, to_square, promotion_piece } => {
|
||||||
|
let promotion_piece = match promotion_piece {
|
||||||
|
Some(piece) => Some(piece.to_index()),
|
||||||
|
None => None
|
||||||
|
};
|
||||||
|
return BitMove::quiet(
|
||||||
|
from_square.to_index(),
|
||||||
|
to_square.to_index(),
|
||||||
|
promotion_piece
|
||||||
|
);
|
||||||
|
},
|
||||||
|
ChessMove::Capture { piece_type, from_square, to_square, captured_piece, promotion_piece } => {
|
||||||
|
let promotion_piece = match promotion_piece {
|
||||||
|
Some(piece) => Some(piece.to_index()),
|
||||||
|
None => None
|
||||||
|
};
|
||||||
|
return BitMove::capture(
|
||||||
|
from_square.to_index(),
|
||||||
|
to_square.to_index(),
|
||||||
|
promotion_piece
|
||||||
|
);
|
||||||
|
},
|
||||||
|
ChessMove::Castle { king_type, king_from, king_to, rook_type, rook_from, rook_to } => {
|
||||||
|
return BitMove::castle(
|
||||||
|
king_from.to_index(),
|
||||||
|
king_to.to_index()
|
||||||
|
);
|
||||||
|
},
|
||||||
|
ChessMove::EnPassant { pawn_type, from_square, to_square, captured_piece, captured_from } => {
|
||||||
|
panic!("ChessMove::to_bitmove was left unimplemented");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return bitmove;
|
||||||
|
}
|
||||||
|
}
|
||||||
9
engine/src/gameend.rs
Normal file
9
engine/src/gameend.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
|
pub enum GameEnd {
|
||||||
|
WhiteWon(String),
|
||||||
|
BlackWon(String),
|
||||||
|
Draw(String),
|
||||||
|
}
|
||||||
|
|
||||||
215
engine/src/lib.rs
Normal file
215
engine/src/lib.rs
Normal file
@@ -0,0 +1,215 @@
|
|||||||
|
mod bitboard;
|
||||||
|
pub mod chessmove;
|
||||||
|
pub mod piecetype;
|
||||||
|
pub mod boardsquare;
|
||||||
|
pub mod movetype;
|
||||||
|
pub mod gameend;
|
||||||
|
|
||||||
|
use chessmove::ChessMove;
|
||||||
|
use gameend::GameEnd;
|
||||||
|
use bitboard::board::Board;
|
||||||
|
use bitboard::movebuffer::MoveBuffer;
|
||||||
|
|
||||||
|
pub fn get_available_moves(fen: &str) -> Vec<ChessMove> {
|
||||||
|
let mut board = Board::build(fen);
|
||||||
|
let mut buffer = MoveBuffer::new();
|
||||||
|
let mut temp_buffer = MoveBuffer::new();
|
||||||
|
let mut generated_moves: Vec<ChessMove> = vec![];
|
||||||
|
|
||||||
|
board.collect_moves(&mut buffer, &mut temp_buffer);
|
||||||
|
|
||||||
|
for idx in 0..buffer.count() {
|
||||||
|
generated_moves.push(ChessMove::from_bitmove(buffer.get(idx), &board));
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("get_available_moves resulted in {} moves", generated_moves.len());
|
||||||
|
return generated_moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_game_over(fen: &str) -> Option<GameEnd> {
|
||||||
|
let mut board = Board::build(fen);
|
||||||
|
let mut buffer = MoveBuffer::new();
|
||||||
|
let mut temp_buffer = MoveBuffer::new();
|
||||||
|
let in_check = board.collect_moves(&mut buffer, &mut temp_buffer);
|
||||||
|
|
||||||
|
println!("is_game_over answered");
|
||||||
|
if buffer.count() > 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if !in_check {
|
||||||
|
return Some(GameEnd::Draw("".to_string()));
|
||||||
|
}
|
||||||
|
return if board.side_to_move() == 0 { Some(GameEnd::BlackWon("".to_string())) } else { Some(GameEnd::WhiteWon("".to_string())) };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_board_after_move(fen: &str, chess_move: &ChessMove) -> String {
|
||||||
|
let mut board = Board::build(fen);
|
||||||
|
let played_move = chess_move.to_bitmove();
|
||||||
|
|
||||||
|
println!("get_board_after_move answered");
|
||||||
|
board.make_move(&played_move);
|
||||||
|
|
||||||
|
return board.fen();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::boardsquare::BoardSquare;
|
||||||
|
use crate::piecetype::PieceType::*;
|
||||||
|
use crate::gameend::GameEnd;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl PartialEq for ChessMove {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
canonical(self) == canonical(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Eq for ChessMove {
|
||||||
|
|
||||||
|
}
|
||||||
|
impl Ord for ChessMove {
|
||||||
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||||
|
let lhs = canonical(self);
|
||||||
|
let rhs = canonical(other);
|
||||||
|
lhs.cmp(&rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialOrd for ChessMove {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for GameEnd {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
match (self, other) {
|
||||||
|
(GameEnd::WhiteWon(a), GameEnd::WhiteWon(b)) => a == b,
|
||||||
|
(GameEnd::BlackWon(a), GameEnd::BlackWon(b)) => a == b,
|
||||||
|
(GameEnd::Draw(a), GameEnd::Draw(b)) => a == b,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn canonical(m: &ChessMove) -> (u8, u8, u8) {
|
||||||
|
match m {
|
||||||
|
ChessMove::Quiet { piece_type, from_square, to_square, promotion_piece } =>
|
||||||
|
(0, from_square.to_index(), to_square.to_index()),
|
||||||
|
ChessMove::Capture { piece_type, from_square, to_square, captured_piece, promotion_piece } =>
|
||||||
|
(1, from_square.to_index(), to_square.to_index()),
|
||||||
|
ChessMove::Castle { king_type, king_from, king_to, rook_type, rook_from, rook_to } =>
|
||||||
|
(2, king_from.to_index(), king_to.to_index()),
|
||||||
|
ChessMove::EnPassant { pawn_type, from_square, to_square, captured_piece, captured_from } =>
|
||||||
|
(3, from_square.to_index(), to_square.to_index()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_available_moves_test() {
|
||||||
|
let boards: [&str; 2] = [
|
||||||
|
"rnbqkbnr/pppppppp/8/1B6/4P3/5P1N/PPPP2PP/RNBQK2R w KQkq e6 0 1",
|
||||||
|
"6Bn/B2Pk3/8/p1r3NK/3p4/b6P/3p2n1/2R5 w - - 0 1"
|
||||||
|
];
|
||||||
|
let mut expected_moves: Vec<Vec<ChessMove>> = vec![
|
||||||
|
vec![
|
||||||
|
ChessMove::capture(WhiteBishop, BoardSquare::from_coord(1, 4), BoardSquare::from_coord(3, 6), BlackPawn, None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(0, 1), BoardSquare::from_coord(0, 2), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(0, 1), BoardSquare::from_coord(0, 3), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(1, 1), BoardSquare::from_coord(1, 2), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(1, 1), BoardSquare::from_coord(1, 3), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(2, 1), BoardSquare::from_coord(2, 2), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(2, 1), BoardSquare::from_coord(2, 3), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(3, 1), BoardSquare::from_coord(3, 2), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(3, 1), BoardSquare::from_coord(3, 3), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(4, 3), BoardSquare::from_coord(4, 4), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(5, 2), BoardSquare::from_coord(5, 3), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(6, 1), BoardSquare::from_coord(6, 2), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(6, 1), BoardSquare::from_coord(6, 3), None),
|
||||||
|
ChessMove::quiet(WhiteKnight, BoardSquare::from_coord(1, 0), BoardSquare::from_coord(0, 2), None),
|
||||||
|
ChessMove::quiet(WhiteKnight, BoardSquare::from_coord(1, 0), BoardSquare::from_coord(2, 2), None),
|
||||||
|
ChessMove::quiet(WhiteKnight, BoardSquare::from_coord(7, 2), BoardSquare::from_coord(6, 0), None),
|
||||||
|
ChessMove::quiet(WhiteKnight, BoardSquare::from_coord(7, 2), BoardSquare::from_coord(5, 1), None),
|
||||||
|
ChessMove::quiet(WhiteKnight, BoardSquare::from_coord(7, 2), BoardSquare::from_coord(5, 3), None),
|
||||||
|
ChessMove::quiet(WhiteKnight, BoardSquare::from_coord(7, 2), BoardSquare::from_coord(6, 4), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(1, 4), BoardSquare::from_coord(5, 0), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(1, 4), BoardSquare::from_coord(4, 1), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(1, 4), BoardSquare::from_coord(3, 2), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(1, 4), BoardSquare::from_coord(2, 3), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(1, 4), BoardSquare::from_coord(0, 3), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(1, 4), BoardSquare::from_coord(0, 5), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(1, 4), BoardSquare::from_coord(2, 5), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(7, 0), BoardSquare::from_coord(6, 0), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(7, 0), BoardSquare::from_coord(5, 0), None),
|
||||||
|
ChessMove::quiet(WhiteQueen, BoardSquare::from_coord(3, 0), BoardSquare::from_coord(4, 1), None),
|
||||||
|
ChessMove::quiet(WhiteKing, BoardSquare::from_coord(4, 0), BoardSquare::from_coord(4, 1), None),
|
||||||
|
ChessMove::quiet(WhiteKing, BoardSquare::from_coord(4, 0), BoardSquare::from_coord(5, 1), None),
|
||||||
|
ChessMove::quiet(WhiteKing, BoardSquare::from_coord(4, 0), BoardSquare::from_coord(5, 0), None),
|
||||||
|
ChessMove::castle(WhiteKing, BoardSquare::from_coord(4, 0), BoardSquare::from_coord(6, 0), WhiteRook, BoardSquare::from_coord(7, 0), BoardSquare::from_coord(5, 0))
|
||||||
|
],
|
||||||
|
vec![
|
||||||
|
ChessMove::capture(WhiteBishop, BoardSquare::from_coord(0, 6), BoardSquare::from_coord(2, 4), BlackRook, None),
|
||||||
|
ChessMove::capture(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(2, 4), BlackRook, None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(7, 2), BoardSquare::from_coord(7, 3), None),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(3, 6), BoardSquare::from_coord(3, 7), Some(WhiteQueen)),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(3, 6), BoardSquare::from_coord(3, 7), Some(WhiteRook)),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(3, 6), BoardSquare::from_coord(3, 7), Some(WhiteBishop)),
|
||||||
|
ChessMove::quiet(WhitePawn, BoardSquare::from_coord(3, 6), BoardSquare::from_coord(3, 7), Some(WhiteKnight)),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(0, 6), BoardSquare::from_coord(1, 5), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(0, 6), BoardSquare::from_coord(1, 7), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(6, 7), BoardSquare::from_coord(7, 6), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(6, 7), BoardSquare::from_coord(5, 6), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(6, 7), BoardSquare::from_coord(4, 5), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(6, 7), BoardSquare::from_coord(3, 4), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(6, 7), BoardSquare::from_coord(2, 3), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(6, 7), BoardSquare::from_coord(1, 2), None),
|
||||||
|
ChessMove::quiet(WhiteBishop, BoardSquare::from_coord(6, 7), BoardSquare::from_coord(0, 1), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(0, 0), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(1, 0), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(3, 0), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(4, 0), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(5, 0), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(6, 0), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(7, 0), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(2, 1), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(2, 2), None),
|
||||||
|
ChessMove::quiet(WhiteRook, BoardSquare::from_coord(2, 0), BoardSquare::from_coord(2, 3), None),
|
||||||
|
ChessMove::quiet(WhiteKing, BoardSquare::from_coord(7, 4), BoardSquare::from_coord(6, 3), None),
|
||||||
|
ChessMove::quiet(WhiteKing, BoardSquare::from_coord(7, 4), BoardSquare::from_coord(7, 5), None)
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
for case in 0..2 {
|
||||||
|
|
||||||
|
let mut generated_moves = get_available_moves(boards[case]);
|
||||||
|
|
||||||
|
generated_moves.sort();
|
||||||
|
expected_moves[case].sort();
|
||||||
|
assert_eq!(generated_moves.len(), expected_moves[case].len());
|
||||||
|
assert_eq!(generated_moves, expected_moves[case]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn is_game_over_test() {
|
||||||
|
|
||||||
|
let boards: [&str; 4] = [
|
||||||
|
"2k5/3pn3/2pP4/1R1P3B/1Np5/3RPp2/1B6/6Kb w - - 0 1",
|
||||||
|
"2K3B1/4P3/8/7p/4pPn1/1N1P1p1p/4bp2/2Rk4 b - - 0 1",
|
||||||
|
"6N1/B2PP3/pR1b4/3P2nb/6P1/3P1k2/2p5/4r1K1 w - - 0 1",
|
||||||
|
"3n1K2/p2k1p2/5P2/b1p2P2/P7/8/3p2r1/8 w - - 0 1"
|
||||||
|
];
|
||||||
|
let expected_results: [Option<GameEnd>; 4] = [
|
||||||
|
None,
|
||||||
|
Some(GameEnd::WhiteWon("".to_string())),
|
||||||
|
Some(GameEnd::BlackWon("".to_string())),
|
||||||
|
Some(GameEnd::Draw("".to_string()))
|
||||||
|
];
|
||||||
|
|
||||||
|
for case in 0..4 {
|
||||||
|
let fen = boards[case];
|
||||||
|
let actual = is_game_over(fen);
|
||||||
|
assert_eq!(actual, expected_results[case]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
mod bitboard;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
|||||||
31
engine/src/movetype.rs
Normal file
31
engine/src/movetype.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
use serde::Serialize;
|
||||||
|
use super::bitboard::bitmove::BitMoveType;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum MoveType {
|
||||||
|
Quiet,
|
||||||
|
Capture,
|
||||||
|
Castle,
|
||||||
|
EnPassant,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MoveType {
|
||||||
|
pub(in super) fn from_bitmovetype(move_type: BitMoveType) -> Self {
|
||||||
|
return match move_type {
|
||||||
|
BitMoveType::Quiet => Self::Quiet,
|
||||||
|
BitMoveType::Capture => Self::Capture,
|
||||||
|
BitMoveType::Castle => Self::Castle,
|
||||||
|
BitMoveType::EnPassant => Self::EnPassant,
|
||||||
|
_ => panic!("invalid move_type index! should NEVER appear")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(in super) fn to_bitmoveType(&self) -> BitMoveType {
|
||||||
|
return match self {
|
||||||
|
&MoveType::Quiet => BitMoveType::Quiet,
|
||||||
|
&MoveType::Capture => BitMoveType::Capture,
|
||||||
|
&MoveType::Castle => BitMoveType::Castle,
|
||||||
|
&MoveType::EnPassant => BitMoveType::EnPassant
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
54
engine/src/piecetype.rs
Normal file
54
engine/src/piecetype.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub enum PieceType {
|
||||||
|
WhitePawn,
|
||||||
|
WhiteKnight,
|
||||||
|
WhiteBishop,
|
||||||
|
WhiteRook,
|
||||||
|
WhiteQueen,
|
||||||
|
WhiteKing,
|
||||||
|
BlackPawn,
|
||||||
|
BlackKnight,
|
||||||
|
BlackBishop,
|
||||||
|
BlackRook,
|
||||||
|
BlackQueen,
|
||||||
|
BlackKing,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PieceType {
|
||||||
|
|
||||||
|
pub(in super) fn from_index(idx: u8) -> Self {
|
||||||
|
return match idx {
|
||||||
|
0 => PieceType::WhitePawn,
|
||||||
|
1 => PieceType::WhiteKnight,
|
||||||
|
2 => PieceType::WhiteBishop,
|
||||||
|
3 => PieceType::WhiteRook,
|
||||||
|
4 => PieceType::WhiteQueen,
|
||||||
|
5 => PieceType::WhiteKing,
|
||||||
|
6 => PieceType::BlackPawn,
|
||||||
|
7 => PieceType::BlackKnight,
|
||||||
|
8 => PieceType::BlackBishop,
|
||||||
|
9 => PieceType::BlackRook,
|
||||||
|
10 => PieceType::BlackQueen,
|
||||||
|
11 => PieceType::BlackKing,
|
||||||
|
_ => panic!("invalid piece index! should NEVER appear")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(in super) fn to_index(&self) -> u8 {
|
||||||
|
return match self {
|
||||||
|
&PieceType::WhitePawn => 0,
|
||||||
|
&PieceType::WhiteKnight => 1,
|
||||||
|
&PieceType::WhiteBishop => 2,
|
||||||
|
&PieceType::WhiteRook => 3,
|
||||||
|
&PieceType::WhiteQueen => 4,
|
||||||
|
&PieceType::WhiteKing => 5,
|
||||||
|
&PieceType::BlackPawn => 6,
|
||||||
|
&PieceType::BlackKnight => 7,
|
||||||
|
&PieceType::BlackBishop => 8,
|
||||||
|
&PieceType::BlackRook => 9,
|
||||||
|
&PieceType::BlackQueen => 10,
|
||||||
|
&PieceType::BlackKing => 11
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1256
server/Cargo.lock
generated
Normal file
1256
server/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,3 +4,21 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
tokio-tungstenite = "0.21"
|
||||||
|
tungstenite = "0.21"
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
futures-util = "0.3.31"
|
||||||
|
url = "2.5.7"
|
||||||
|
uuid = {version = "1.18.1", features = ["v4", "serde"] }
|
||||||
|
anyhow = "1.0.100"
|
||||||
|
rand = "0.9.2"
|
||||||
|
engine = {path = "../engine/"}
|
||||||
|
log = {version = "0.4.28"}
|
||||||
|
env_logger = "0.11.8"
|
||||||
|
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "client"
|
||||||
|
path = "src/bin/client.rs"
|
||||||
|
|||||||
228
server/src/bin/client.rs
Normal file
228
server/src/bin/client.rs
Normal file
@@ -0,0 +1,228 @@
|
|||||||
|
use engine::gameend::GameEnd;
|
||||||
|
use engine::{boardsquare::BoardSquare, chessmove::ChessMove, piecetype::PieceType};
|
||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::io::{self, Write};
|
||||||
|
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
||||||
|
use url::Url;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
enum ClientMessage {
|
||||||
|
Join { username: String },
|
||||||
|
FindMatch,
|
||||||
|
Move { step: ChessMove, fen: String },
|
||||||
|
Resign,
|
||||||
|
Chat { text: String },
|
||||||
|
RequestLegalMoves { fen: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum ServerMessage2 {
|
||||||
|
GameEnd {
|
||||||
|
winner: GameEnd,
|
||||||
|
},
|
||||||
|
UIUpdate {
|
||||||
|
fen: String,
|
||||||
|
},
|
||||||
|
MatchFound {
|
||||||
|
match_id: Uuid,
|
||||||
|
color: String,
|
||||||
|
opponent_name: String,
|
||||||
|
},
|
||||||
|
Ok {
|
||||||
|
response: Result<(), String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("Knightly Chess Client");
|
||||||
|
println!("========================");
|
||||||
|
|
||||||
|
// Get server address from user
|
||||||
|
print!("Enter server address [ws://127.0.0.1:9001]: ");
|
||||||
|
io::stdout().flush()?;
|
||||||
|
let mut server_addr = String::new();
|
||||||
|
io::stdin().read_line(&mut server_addr)?;
|
||||||
|
let server_addr = server_addr.trim();
|
||||||
|
let server_addr = if server_addr.is_empty() {
|
||||||
|
"ws://127.0.0.1:9001".to_string()
|
||||||
|
} else {
|
||||||
|
server_addr.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Connect to server
|
||||||
|
println!("Connecting to {}...", server_addr);
|
||||||
|
let url = Url::parse(&server_addr)?;
|
||||||
|
let (ws_stream, _) = connect_async(url).await?;
|
||||||
|
println!("Connected to server!");
|
||||||
|
|
||||||
|
let (mut write, mut read) = ws_stream.split();
|
||||||
|
|
||||||
|
// Spawn a task to handle incoming messages
|
||||||
|
let read_handle = tokio::spawn(async move {
|
||||||
|
while let Some(message) = read.next().await {
|
||||||
|
match message {
|
||||||
|
Ok(msg) => {
|
||||||
|
if msg.is_text() {
|
||||||
|
let text = msg.to_text().unwrap();
|
||||||
|
println!("\nServer: {}", text);
|
||||||
|
|
||||||
|
// Try to parse as structured message
|
||||||
|
if let Ok(parsed) = serde_json::from_str::<ServerMessage2>(text) {
|
||||||
|
match parsed {
|
||||||
|
ServerMessage2::MatchFound {
|
||||||
|
match_id,
|
||||||
|
color,
|
||||||
|
opponent_name,
|
||||||
|
} => {
|
||||||
|
println!(
|
||||||
|
"opponent: {}, match_id: {}, color: {}",
|
||||||
|
opponent_name, match_id, color
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("cucc");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error receiving message: {}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Main loop for sending messages
|
||||||
|
println!("\nAvailable commands:");
|
||||||
|
println!(" join <username> - Join the server");
|
||||||
|
println!(" findmatch - Find a match");
|
||||||
|
println!(" move <from> <to> - Make a move (e.g., move e2 e4)");
|
||||||
|
println!(" chat <message> - Send chat message");
|
||||||
|
println!(" resign - Resign from current game");
|
||||||
|
println!(" quit - Exit client");
|
||||||
|
println!();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
print!("➡️ Enter command: ");
|
||||||
|
io::stdout().flush()?;
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin().read_line(&mut input)?;
|
||||||
|
let input = input.trim();
|
||||||
|
|
||||||
|
if input.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let parts: Vec<&str> = input.split_whitespace().collect();
|
||||||
|
let command = parts[0].to_lowercase();
|
||||||
|
|
||||||
|
match command.as_str() {
|
||||||
|
"quit" | "exit" => {
|
||||||
|
println!("👋 Goodbye!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
"join" => {
|
||||||
|
if parts.len() >= 2 {
|
||||||
|
let username = parts[1..].join(" ");
|
||||||
|
let message = ClientMessage::Join { username };
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
} else {
|
||||||
|
println!("Usage: join <username>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"findmatch" | "find" => {
|
||||||
|
let message = ClientMessage::FindMatch;
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
println!("🔍 Searching for a match...");
|
||||||
|
}
|
||||||
|
"move" => {
|
||||||
|
if parts.len() >= 3 {
|
||||||
|
//let from = parts[1].to_string();
|
||||||
|
//let to = parts[2].to_string();
|
||||||
|
let fen = parts[1].to_string();
|
||||||
|
|
||||||
|
let step = ChessMove::quiet(
|
||||||
|
engine::piecetype::PieceType::WhiteBishop,
|
||||||
|
BoardSquare::new(),
|
||||||
|
BoardSquare { x: 1, y: 1 },
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
let message = ClientMessage::Move { step, fen };
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
//println!("♟️ Sent move: {} -> {}", parts[1], parts[2]);
|
||||||
|
} else {
|
||||||
|
println!("Usage: move <from> <to> (e.g., move e2 e4)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"chat" => {
|
||||||
|
if parts.len() >= 2 {
|
||||||
|
let text = parts[1..].join(" ");
|
||||||
|
let message = ClientMessage::Chat { text };
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
} else {
|
||||||
|
println!("Usage: chat <message>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"resign" => {
|
||||||
|
let message = ClientMessage::Resign;
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
println!("Resigned from current game");
|
||||||
|
}
|
||||||
|
"help" => {
|
||||||
|
print_help();
|
||||||
|
}
|
||||||
|
"requestmoves" => {
|
||||||
|
if parts.len() >= 2 {
|
||||||
|
let fen = parts[1..].join(" ");
|
||||||
|
let message = ClientMessage::RequestLegalMoves { fen };
|
||||||
|
send_message(&mut write, &message).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!(
|
||||||
|
"Unknown command: {}. Type 'help' for available commands.",
|
||||||
|
command
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
read_handle.abort();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn send_message(
|
||||||
|
write: &mut futures_util::stream::SplitSink<
|
||||||
|
tokio_tungstenite::WebSocketStream<
|
||||||
|
tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
|
||||||
|
>,
|
||||||
|
Message,
|
||||||
|
>,
|
||||||
|
message: &ClientMessage,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let json = serde_json::to_string(message)?;
|
||||||
|
write.send(Message::Text(json)).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_help() {
|
||||||
|
println!("\n📖 Available Commands:");
|
||||||
|
println!(" join <username> - Register with a username");
|
||||||
|
println!(" findmatch - Enter matchmaking queue");
|
||||||
|
println!(" move <from> <to> - Make a chess move");
|
||||||
|
println!(" chat <message> - Send chat to opponent");
|
||||||
|
println!(" resign - Resign from current game");
|
||||||
|
println!(" help - Show this help");
|
||||||
|
println!(" quit - Exit the client");
|
||||||
|
println!(" requestmoves - Request the legal moves");
|
||||||
|
println!();
|
||||||
|
}
|
||||||
439
server/src/connection.rs
Normal file
439
server/src/connection.rs
Normal file
@@ -0,0 +1,439 @@
|
|||||||
|
use crate::connection::ClientEvent::*;
|
||||||
|
use crate::matchmaking;
|
||||||
|
use engine::chessmove::ChessMove;
|
||||||
|
use engine::gameend::GameEnd::{self, *};
|
||||||
|
use engine::{get_available_moves, is_game_over};
|
||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use log::{error, info, warn};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use tokio_tungstenite::{WebSocketStream, tungstenite::Message};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
// Type definitions
|
||||||
|
pub type Tx = futures_util::stream::SplitSink<WebSocketStream<TcpStream>, Message>;
|
||||||
|
pub type ConnectionMap = Arc<Mutex<HashMap<Uuid, PlayerConnection>>>;
|
||||||
|
pub type MatchMap = Arc<Mutex<HashMap<Uuid, GameMatch>>>;
|
||||||
|
pub type WaitingQueue = Arc<Mutex<VecDeque<Uuid>>>;
|
||||||
|
|
||||||
|
pub async fn clean_up_match(matches: &MatchMap, match_id: &Uuid) {
|
||||||
|
matches.lock().await.remove(&match_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper functions to create new instances
|
||||||
|
pub fn new_connection_map() -> ConnectionMap {
|
||||||
|
warn!("Created new connection map");
|
||||||
|
Arc::new(Mutex::new(HashMap::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_match_map() -> MatchMap {
|
||||||
|
warn!("Created new match map");
|
||||||
|
Arc::new(Mutex::new(HashMap::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_waiting_queue() -> WaitingQueue {
|
||||||
|
warn!("Created new waiting queue");
|
||||||
|
Arc::new(Mutex::new(VecDeque::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct Step {
|
||||||
|
pub from: String,
|
||||||
|
pub to: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum ServerMessage2 {
|
||||||
|
GameEnd {
|
||||||
|
winner: GameEnd,
|
||||||
|
},
|
||||||
|
UIUpdate {
|
||||||
|
fen: String,
|
||||||
|
},
|
||||||
|
MatchFound {
|
||||||
|
match_id: Uuid,
|
||||||
|
color: String,
|
||||||
|
opponent_name: String,
|
||||||
|
},
|
||||||
|
Ok {
|
||||||
|
response: Result<(), String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
enum ClientEvent {
|
||||||
|
Join { username: String },
|
||||||
|
FindMatch,
|
||||||
|
Move { step: ChessMove },
|
||||||
|
Resign,
|
||||||
|
Chat { text: String },
|
||||||
|
RequestLegalMoves { fen: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct PlayerConnection {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub username: Option<String>,
|
||||||
|
pub tx: Tx,
|
||||||
|
pub current_match: Option<Uuid>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct GameMatch {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub player_white: Uuid,
|
||||||
|
pub player_black: Uuid,
|
||||||
|
pub board_state: String,
|
||||||
|
pub move_history: Vec<Step>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Message sending utilities
|
||||||
|
pub async fn send_message_to_player_connection(
|
||||||
|
connection: Option<&mut PlayerConnection>,
|
||||||
|
message: &str,
|
||||||
|
) -> Result<(), tokio_tungstenite::tungstenite::Error> {
|
||||||
|
match connection {
|
||||||
|
Some(connection) => {
|
||||||
|
info!("sending message to: {}", connection.id);
|
||||||
|
connection.tx.send(Message::Text(message.to_string())).await
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
error!("No connection provided");
|
||||||
|
Err(tokio_tungstenite::tungstenite::Error::ConnectionClosed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn broadcast_to_all(connections: &ConnectionMap, message: &str) {
|
||||||
|
let mut connections_lock = connections.lock().await;
|
||||||
|
let mut dead_connections = Vec::new();
|
||||||
|
|
||||||
|
for (id, connection) in connections_lock.iter_mut() {
|
||||||
|
if let Err(e) = connection.tx.send(Message::Text(message.to_string())).await {
|
||||||
|
error!("Failed to send to {}: {}", id, e);
|
||||||
|
dead_connections.push(*id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up dead connections
|
||||||
|
for dead_id in dead_connections {
|
||||||
|
connections_lock.remove(&dead_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn broadcast_to_match(
|
||||||
|
connections: &ConnectionMap,
|
||||||
|
matches: &MatchMap,
|
||||||
|
match_id: Uuid,
|
||||||
|
message: &str,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
info!("Broadcasting data to match: {}", &match_id);
|
||||||
|
let matches_lock = matches.lock().await;
|
||||||
|
if let Some(game_match) = matches_lock.get(&match_id) {
|
||||||
|
send_message_to_player_connection(
|
||||||
|
connections.lock().await.get_mut(&game_match.player_white),
|
||||||
|
message,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
send_message_to_player_connection(
|
||||||
|
connections.lock().await.get_mut(&game_match.player_black),
|
||||||
|
message,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connection handler
|
||||||
|
pub async fn handle_connection(
|
||||||
|
stream: TcpStream,
|
||||||
|
connections: ConnectionMap,
|
||||||
|
matches: MatchMap,
|
||||||
|
waiting_queue: WaitingQueue,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
use tokio_tungstenite::accept_async;
|
||||||
|
|
||||||
|
let ws_stream = accept_async(stream).await?;
|
||||||
|
let (write, mut read) = ws_stream.split();
|
||||||
|
warn!("Accepted new connection");
|
||||||
|
|
||||||
|
let player_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
// Store the connection
|
||||||
|
{
|
||||||
|
let mut conn_map = connections.lock().await;
|
||||||
|
conn_map.insert(
|
||||||
|
player_id,
|
||||||
|
PlayerConnection {
|
||||||
|
id: player_id,
|
||||||
|
username: None,
|
||||||
|
tx: write,
|
||||||
|
current_match: None,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("id: {}", &player_id);
|
||||||
|
|
||||||
|
// Message processing loop
|
||||||
|
while let Some(Ok(message)) = read.next().await {
|
||||||
|
if message.is_text() {
|
||||||
|
let text = message.to_text()?;
|
||||||
|
info!("Received from {}: {}", player_id, text);
|
||||||
|
|
||||||
|
let client_data: ClientEvent = serde_json::from_str(text)
|
||||||
|
.expect("Failed to convert data into json at handle_connection");
|
||||||
|
|
||||||
|
match client_data {
|
||||||
|
Join { username } => {
|
||||||
|
{
|
||||||
|
let mut conn_map = connections.lock().await;
|
||||||
|
let player = conn_map.get_mut(&player_id).unwrap();
|
||||||
|
player.username = Some(username.clone());
|
||||||
|
info!("player: {}, set username: {}", &player_id, username);
|
||||||
|
}
|
||||||
|
|
||||||
|
//respone to client
|
||||||
|
let response = ServerMessage2::Ok { response: Ok(()) };
|
||||||
|
|
||||||
|
let mut conn_map = connections.lock().await;
|
||||||
|
let _ = send_message_to_player_connection(
|
||||||
|
conn_map.get_mut(&player_id),
|
||||||
|
&serde_json::to_string(&response).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
FindMatch => {
|
||||||
|
let mut wait_queue = waiting_queue.lock().await;
|
||||||
|
wait_queue.push_back(player_id.clone());
|
||||||
|
info!("Appended {} to the waiting queue", player_id);
|
||||||
|
info!("queue {:?}", wait_queue);
|
||||||
|
}
|
||||||
|
Move { step } => {
|
||||||
|
let match_id = connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
{
|
||||||
|
info!("updating board state in match: {}", &match_id);
|
||||||
|
let mut matches = matches.lock().await;
|
||||||
|
matches.get_mut(&match_id).unwrap().board_state =
|
||||||
|
engine::get_board_after_move(
|
||||||
|
&matches.get(&match_id).unwrap().board_state,
|
||||||
|
&step,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let message = ServerMessage2::UIUpdate {
|
||||||
|
fen: matches
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&match_id)
|
||||||
|
.unwrap()
|
||||||
|
.board_state
|
||||||
|
.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = broadcast_to_match(
|
||||||
|
&connections,
|
||||||
|
&matches,
|
||||||
|
match_id,
|
||||||
|
&serde_json::to_string(&message).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
{
|
||||||
|
let is_game_end = engine::is_game_over(
|
||||||
|
&matches.lock().await.get(&match_id).unwrap().board_state,
|
||||||
|
);
|
||||||
|
|
||||||
|
match is_game_end {
|
||||||
|
Some(res) => {
|
||||||
|
warn!("A player won the match: {}", &match_id);
|
||||||
|
let message = ServerMessage2::GameEnd { winner: res };
|
||||||
|
let _ = broadcast_to_match(
|
||||||
|
&connections,
|
||||||
|
&matches,
|
||||||
|
match_id,
|
||||||
|
&serde_json::to_string(&message).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
clean_up_match(&matches, &match_id);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
info!("No winner match continues. Id: {}", &match_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RequestLegalMoves { fen } => {
|
||||||
|
info!("Requesting legal moves player: {}", &player_id);
|
||||||
|
let moves = get_available_moves(&fen);
|
||||||
|
let _ = send_message_to_player_connection(
|
||||||
|
connections.lock().await.get_mut(&player_id),
|
||||||
|
&serde_json::to_string(&moves).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
info!("Sent moves to player: {}", player_id);
|
||||||
|
}
|
||||||
|
Resign => {
|
||||||
|
warn!("Resigned!");
|
||||||
|
let (fuck, fuck_id): (ServerMessage2, &Uuid) = {
|
||||||
|
let matches = matches.lock().await;
|
||||||
|
|
||||||
|
let curr_match = matches
|
||||||
|
.get(
|
||||||
|
&connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if player_id == curr_match.player_white {
|
||||||
|
(
|
||||||
|
ServerMessage2::GameEnd {
|
||||||
|
winner: GameEnd::BlackWon("Resigned".to_string()),
|
||||||
|
},
|
||||||
|
&connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
ServerMessage2::GameEnd {
|
||||||
|
winner: GameEnd::WhiteWon("Resigned".to_string()),
|
||||||
|
},
|
||||||
|
&connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
broadcast_to_match(
|
||||||
|
&connections,
|
||||||
|
&matches,
|
||||||
|
connections
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.get(&player_id)
|
||||||
|
.unwrap()
|
||||||
|
.current_match
|
||||||
|
.unwrap(),
|
||||||
|
&serde_json::to_string(&fuck).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
clean_up_match(&matches, fuck_id);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
warn!("Not known client event");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup on disconnect
|
||||||
|
cleanup_player(player_id, &connections, &matches, &waiting_queue).await;
|
||||||
|
warn!("Connection {} closed", player_id);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup_player(
|
||||||
|
player_id: Uuid,
|
||||||
|
connections: &ConnectionMap,
|
||||||
|
_matches: &MatchMap,
|
||||||
|
waiting_queue: &WaitingQueue,
|
||||||
|
) {
|
||||||
|
// Remove from waiting queue
|
||||||
|
waiting_queue.lock().await.retain(|&id| id != player_id);
|
||||||
|
|
||||||
|
// Remove from connections
|
||||||
|
connections.lock().await.remove(&player_id);
|
||||||
|
|
||||||
|
warn!("Cleaned up player {}", player_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_send_message_to_nonexistent_player() {
|
||||||
|
let connections = new_connection_map();
|
||||||
|
let player_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
// Test 1: Pass None directly (non-existent player)
|
||||||
|
let result = send_message_to_player_connection(None, "test message").await;
|
||||||
|
|
||||||
|
assert!(result.is_err(), "Should return error for None connection");
|
||||||
|
println!("Test passed: Handles None connection correctly");
|
||||||
|
|
||||||
|
// Test 2: Try to get non-existent player from map
|
||||||
|
let mut conn = connections.lock().await;
|
||||||
|
let non_existent_connection = conn.get_mut(&player_id); // This will be None
|
||||||
|
|
||||||
|
let result2 =
|
||||||
|
send_message_to_player_connection(non_existent_connection, "test message").await;
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result2.is_err(),
|
||||||
|
"Should return error for non-existent player"
|
||||||
|
);
|
||||||
|
println!("Test passed: Handles non-existent player in map correctly");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_broadcast_to_empty_connections() {
|
||||||
|
let connections = new_connection_map();
|
||||||
|
|
||||||
|
broadcast_to_all(&connections, "test broadcast").await;
|
||||||
|
|
||||||
|
let conn_map = connections.lock().await;
|
||||||
|
assert!(conn_map.is_empty(), "Connections should still be empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_connection_cleanup() {
|
||||||
|
let connections = new_connection_map();
|
||||||
|
let matches = new_match_map();
|
||||||
|
let waiting_queue = new_waiting_queue();
|
||||||
|
|
||||||
|
let player_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
{
|
||||||
|
waiting_queue.lock().await.push_back(player_id);
|
||||||
|
assert_eq!(waiting_queue.lock().await.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup_player(player_id, &connections, &matches, &waiting_queue).await;
|
||||||
|
|
||||||
|
{
|
||||||
|
let queue = waiting_queue.lock().await;
|
||||||
|
assert!(
|
||||||
|
!queue.contains(&player_id),
|
||||||
|
"Player should be removed from waiting queue"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,47 @@
|
|||||||
fn main() {
|
mod connection;
|
||||||
println!("Hello, world!");
|
mod matchmaking;
|
||||||
|
use env_logger::Env;
|
||||||
|
use log::{error, info};
|
||||||
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
let env = Env::default().filter_or("MY_LOG_LEVEL", "INFO");
|
||||||
|
env_logger::init_from_env(env);
|
||||||
|
|
||||||
|
let address = "0.0.0.0:9001";
|
||||||
|
let listener = TcpListener::bind(address).await?;
|
||||||
|
info!("Server running on ws://{}", address);
|
||||||
|
|
||||||
|
// Shared state initialization using the new helper functions
|
||||||
|
let connections = connection::new_connection_map();
|
||||||
|
let matches = connection::new_match_map();
|
||||||
|
let waiting_queue = connection::new_waiting_queue();
|
||||||
|
|
||||||
|
// Start matchmaking background task
|
||||||
|
let matchmaker = matchmaking::MatchmakingSystem::new(
|
||||||
|
connections.clone(),
|
||||||
|
matches.clone(),
|
||||||
|
waiting_queue.clone(),
|
||||||
|
);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
matchmaker.run().await;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Main connection loop
|
||||||
|
while let Ok((stream, _)) = listener.accept().await {
|
||||||
|
let connections = connections.clone();
|
||||||
|
let matches = matches.clone();
|
||||||
|
let waiting_queue = waiting_queue.clone();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if let Err(e) =
|
||||||
|
connection::handle_connection(stream, connections, matches, waiting_queue).await
|
||||||
|
{
|
||||||
|
error!("Connection error: {}", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
209
server/src/matchmaking.rs
Normal file
209
server/src/matchmaking.rs
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
use crate::connection::ServerMessage2;
|
||||||
|
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue};
|
||||||
|
use log::{error, info, warn};
|
||||||
|
use rand::random;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub struct MatchmakingSystem {
|
||||||
|
connections: ConnectionMap,
|
||||||
|
matches: MatchMap,
|
||||||
|
waiting_queue: WaitingQueue,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MatchmakingSystem {
|
||||||
|
pub fn new(connections: ConnectionMap, matches: MatchMap, waiting_queue: WaitingQueue) -> Self {
|
||||||
|
Self {
|
||||||
|
connections,
|
||||||
|
matches,
|
||||||
|
waiting_queue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(&self) {
|
||||||
|
loop {
|
||||||
|
self.try_create_match().await;
|
||||||
|
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn try_create_match(&self) {
|
||||||
|
//info!("Checking for new matches!");
|
||||||
|
let mut queue = self.waiting_queue.lock().await;
|
||||||
|
|
||||||
|
while queue.len() >= 2 {
|
||||||
|
let player1 = queue.pop_front().unwrap();
|
||||||
|
let player2 = queue.pop_front().unwrap();
|
||||||
|
|
||||||
|
info!("Creating new match. Players: {}, {}", &player1, &player2);
|
||||||
|
|
||||||
|
let match_id = Uuid::new_v4();
|
||||||
|
let (white_player, black_player) = if random::<bool>() {
|
||||||
|
info!("player1 is white, player2 is black");
|
||||||
|
(player1, player2)
|
||||||
|
} else {
|
||||||
|
info!("player2 is white, player1 is black");
|
||||||
|
(player2, player1)
|
||||||
|
};
|
||||||
|
|
||||||
|
let game_match = GameMatch {
|
||||||
|
id: match_id,
|
||||||
|
player_white: white_player,
|
||||||
|
player_black: black_player,
|
||||||
|
board_state: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".to_string(),
|
||||||
|
move_history: Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
info!("Match id: {}", &game_match.id);
|
||||||
|
|
||||||
|
// Store the match
|
||||||
|
self.matches.lock().await.insert(match_id, game_match);
|
||||||
|
|
||||||
|
// Update player connections
|
||||||
|
{
|
||||||
|
let mut conn_map = self.connections.lock().await;
|
||||||
|
if let Some(player) = conn_map.get_mut(&white_player) {
|
||||||
|
player.current_match = Some(match_id);
|
||||||
|
} else {
|
||||||
|
error!("Could not store match id for white player");
|
||||||
|
}
|
||||||
|
if let Some(player) = conn_map.get_mut(&black_player) {
|
||||||
|
player.current_match = Some(match_id);
|
||||||
|
//TODO: at the end of a match delete this from player
|
||||||
|
} else {
|
||||||
|
error!("Could not store match id for black player");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify players
|
||||||
|
info!(
|
||||||
|
"Notifying player for a match: {:?} | {:?}",
|
||||||
|
white_player, black_player
|
||||||
|
);
|
||||||
|
self.notify_players(white_player, black_player, match_id)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn notify_players(&self, white: Uuid, black: Uuid, match_id: Uuid) {
|
||||||
|
let mut conn_map = self.connections.lock().await;
|
||||||
|
|
||||||
|
// Notify white player
|
||||||
|
if let Some(_) = conn_map.get(&white) {
|
||||||
|
let message = ServerMessage2::MatchFound {
|
||||||
|
match_id: match_id.clone(),
|
||||||
|
color: String::from("white"),
|
||||||
|
opponent_name: conn_map
|
||||||
|
.get(&white)
|
||||||
|
.and_then(|c| c.username.as_deref())
|
||||||
|
.unwrap_or("Opponent")
|
||||||
|
.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = crate::connection::send_message_to_player_connection(
|
||||||
|
conn_map.get_mut(&white),
|
||||||
|
&serde_json::to_string(&message).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify black player
|
||||||
|
if let Some(_) = conn_map.get(&black) {
|
||||||
|
let message = ServerMessage2::MatchFound {
|
||||||
|
match_id: match_id.clone(),
|
||||||
|
color: String::from("black"),
|
||||||
|
opponent_name: conn_map
|
||||||
|
.get(&black)
|
||||||
|
.and_then(|c| c.username.as_deref())
|
||||||
|
.unwrap_or("Opponent")
|
||||||
|
.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = crate::connection::send_message_to_player_connection(
|
||||||
|
conn_map.get_mut(&black),
|
||||||
|
&serde_json::to_string(&message).unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Match created: {} (white) vs {} (black)", white, black);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::connection::new_connection_map;
|
||||||
|
use crate::connection::new_match_map;
|
||||||
|
use crate::connection::new_waiting_queue;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_matchmaking_creates_matches() {
|
||||||
|
let connections = new_connection_map();
|
||||||
|
let matches = new_match_map();
|
||||||
|
let waiting_queue = new_waiting_queue();
|
||||||
|
|
||||||
|
let matchmaking =
|
||||||
|
MatchmakingSystem::new(connections.clone(), matches.clone(), waiting_queue.clone());
|
||||||
|
|
||||||
|
let player1 = Uuid::new_v4();
|
||||||
|
let player2 = Uuid::new_v4();
|
||||||
|
|
||||||
|
{
|
||||||
|
waiting_queue.lock().await.push_back(player1);
|
||||||
|
waiting_queue.lock().await.push_back(player2);
|
||||||
|
}
|
||||||
|
|
||||||
|
matchmaking.try_create_match().await;
|
||||||
|
|
||||||
|
{
|
||||||
|
let matches_map = matches.lock().await;
|
||||||
|
assert_eq!(matches_map.len(), 1, "Should create one match");
|
||||||
|
|
||||||
|
let game_match = matches_map.values().next().unwrap();
|
||||||
|
assert!(game_match.player_white == player1 || game_match.player_white == player2);
|
||||||
|
assert!(game_match.player_black == player1 || game_match.player_black == player2);
|
||||||
|
assert_ne!(
|
||||||
|
game_match.player_white, game_match.player_black,
|
||||||
|
"Players should be different"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let queue = waiting_queue.lock().await;
|
||||||
|
assert!(
|
||||||
|
queue.is_empty(),
|
||||||
|
"Waiting queue should be empty after matchmaking"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_matchmaking_with_odd_players() {
|
||||||
|
let connections = new_connection_map();
|
||||||
|
let matches = new_match_map();
|
||||||
|
let waiting_queue = new_waiting_queue();
|
||||||
|
|
||||||
|
let matchmaking =
|
||||||
|
MatchmakingSystem::new(connections.clone(), matches.clone(), waiting_queue.clone());
|
||||||
|
|
||||||
|
let player1 = Uuid::new_v4();
|
||||||
|
{
|
||||||
|
waiting_queue.lock().await.push_back(player1);
|
||||||
|
}
|
||||||
|
|
||||||
|
matchmaking.try_create_match().await;
|
||||||
|
|
||||||
|
{
|
||||||
|
let matches_map = matches.lock().await;
|
||||||
|
assert!(
|
||||||
|
matches_map.is_empty(),
|
||||||
|
"Should not create match with only one player"
|
||||||
|
);
|
||||||
|
|
||||||
|
let queue = waiting_queue.lock().await;
|
||||||
|
assert_eq!(queue.len(), 1, "Should keep single player in queue");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4371
ui/Cargo.lock
generated
Normal file
4371
ui/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,3 +4,19 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
eframe = "0.33.0"
|
||||||
|
egui = "0.33.0"
|
||||||
|
winit = "0.30.12"
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
tokio-tungstenite = "0.21"
|
||||||
|
tungstenite = "0.21"
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
futures-util = "0.3.31"
|
||||||
|
url = "2.5.7"
|
||||||
|
uuid = {version = "1.18.1", features = ["v4", "serde"] }
|
||||||
|
engine = {path = "../engine/"}
|
||||||
|
log = {version = "0.4.28"}
|
||||||
|
env_logger = "0.11.8"
|
||||||
|
local-ip-address = "0.6.5"
|
||||||
|
anyhow = "1.0.100"
|
||||||
|
|||||||
BIN
ui/fonts/DejaVuSans.ttf
Normal file
BIN
ui/fonts/DejaVuSans.ttf
Normal file
Binary file not shown.
117
ui/src/connection.rs
Normal file
117
ui/src/connection.rs
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
use engine::{chessmove::ChessMove, gameend::GameEnd};
|
||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use local_ip_address::local_ip;
|
||||||
|
use log::{error, info, warn};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{
|
||||||
|
net::{IpAddr, Ipv4Addr},
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
use tokio_tungstenite::connect_async;
|
||||||
|
use tungstenite::Message;
|
||||||
|
use url::Url;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{ChessApp, ClientEvent, SharedGameState};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub enum ServerMessage2 {
|
||||||
|
GameEnd {
|
||||||
|
winner: GameEnd,
|
||||||
|
},
|
||||||
|
UIUpdate {
|
||||||
|
fen: String,
|
||||||
|
},
|
||||||
|
MatchFound {
|
||||||
|
match_id: Uuid,
|
||||||
|
color: String,
|
||||||
|
opponent_name: String,
|
||||||
|
},
|
||||||
|
Ok {
|
||||||
|
response: Result<(), String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
enum ClientMessage {
|
||||||
|
Join { username: String },
|
||||||
|
FindMatch,
|
||||||
|
Move { step: ChessMove, fen: String },
|
||||||
|
Resign,
|
||||||
|
Chat { text: String },
|
||||||
|
RequestLegalMoves { fen: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_ip_address() -> IpAddr {
|
||||||
|
let ip = local_ip().unwrap_or(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)));
|
||||||
|
|
||||||
|
ip
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_connection(
|
||||||
|
server_port: &str,
|
||||||
|
shared_state: SharedGameState,
|
||||||
|
ui_events: Arc<Mutex<Vec<ClientEvent>>>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let address = get_ip_address();
|
||||||
|
|
||||||
|
//start main loop
|
||||||
|
let server_address = String::from("ws://") + &address.to_string() + ":" + server_port;
|
||||||
|
warn!(
|
||||||
|
"Machine IpAddress is bound for listener. Ip: {}",
|
||||||
|
server_address
|
||||||
|
);
|
||||||
|
|
||||||
|
let url = Url::parse(&server_address)?;
|
||||||
|
|
||||||
|
let (ws_stream, _) = connect_async(url).await?;
|
||||||
|
let (mut write, mut read) = ws_stream.split();
|
||||||
|
|
||||||
|
while let Some(message) = read.next().await {
|
||||||
|
info!("connection");
|
||||||
|
match message {
|
||||||
|
Ok(msg) => {
|
||||||
|
if msg.is_text() {
|
||||||
|
let text = msg.to_text().unwrap();
|
||||||
|
info!("text: {}", text);
|
||||||
|
|
||||||
|
/*if let Ok(parsed) = serde_json::from_str::<ServerMessage2>(text) {
|
||||||
|
match parsed {
|
||||||
|
ServerMessage2::GameEnd { winner } => {}
|
||||||
|
ServerMessage2::UIUpdate { fen } => {}
|
||||||
|
ServerMessage2::MatchFound {
|
||||||
|
match_id,
|
||||||
|
color,
|
||||||
|
opponent_name,
|
||||||
|
} => {
|
||||||
|
//chess_app.player_color = Some(color);
|
||||||
|
}
|
||||||
|
ServerMessage2::Ok { response } => {}
|
||||||
|
_ => {
|
||||||
|
error!("Received unkown servermessage2");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
if let Ok(parsed) = serde_json::from_str::<ServerMessage2>(text) {
|
||||||
|
// Update shared state with server message
|
||||||
|
shared_state.update_from_server_message(parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send UI events to server
|
||||||
|
let events = ui_events.lock().unwrap().drain(..).collect::<Vec<_>>();
|
||||||
|
for event in events {
|
||||||
|
let message = serde_json::to_string(&event)?;
|
||||||
|
write.send(Message::Text(message)).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error receiving message: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
904
ui/src/main.rs
904
ui/src/main.rs
@@ -1,3 +1,903 @@
|
|||||||
fn main() {
|
use eframe::egui;
|
||||||
println!("Hello, world!");
|
use engine::{boardsquare::BoardSquare, chessmove::ChessMove};
|
||||||
|
use env_logger::Env;
|
||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use log::{error, info, warn};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
||||||
|
use url::Url;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<(), eframe::Error> {
|
||||||
|
// Set up logging
|
||||||
|
let env = Env::default().filter_or("MY_LOG_LEVEL", "INFO");
|
||||||
|
env_logger::init_from_env(env);
|
||||||
|
warn!("Initialized logger");
|
||||||
|
|
||||||
|
let options = eframe::NativeOptions {
|
||||||
|
viewport: egui::ViewportBuilder::default()
|
||||||
|
.with_fullscreen(false)
|
||||||
|
.with_min_inner_size(egui::vec2(800.0, 800.0))
|
||||||
|
.with_inner_size(egui::vec2(1920.0, 1080.0)),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
eframe::run_native(
|
||||||
|
"Knightly",
|
||||||
|
options,
|
||||||
|
Box::new(|cc| {
|
||||||
|
let mut fonts = egui::FontDefinitions::default();
|
||||||
|
fonts.font_data.insert(
|
||||||
|
"symbols".to_owned(),
|
||||||
|
egui::FontData::from_static(include_bytes!("../fonts/DejaVuSans.ttf")).into(),
|
||||||
|
);
|
||||||
|
fonts
|
||||||
|
.families
|
||||||
|
.entry(egui::FontFamily::Proportional)
|
||||||
|
.or_default()
|
||||||
|
.insert(0, "symbols".to_owned());
|
||||||
|
cc.egui_ctx.set_fonts(fonts);
|
||||||
|
Ok(Box::new(ChessApp::default()))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub enum ServerMessage2 {
|
||||||
|
GameEnd {
|
||||||
|
winner: String,
|
||||||
|
},
|
||||||
|
UIUpdate {
|
||||||
|
fen: String,
|
||||||
|
},
|
||||||
|
MatchFound {
|
||||||
|
match_id: Uuid,
|
||||||
|
color: String,
|
||||||
|
opponent_name: String,
|
||||||
|
},
|
||||||
|
Ok {
|
||||||
|
response: Result<(), String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
pub enum ClientEvent {
|
||||||
|
Join { username: String },
|
||||||
|
FindMatch,
|
||||||
|
Move { step: ChessMove },
|
||||||
|
Resign,
|
||||||
|
Chat { text: String },
|
||||||
|
RequestLegalMoves { fen: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
// Game state
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct GameState {
|
||||||
|
fen: String,
|
||||||
|
player_color: Option<String>,
|
||||||
|
opponent_name: Option<String>,
|
||||||
|
match_id: Option<Uuid>,
|
||||||
|
game_over: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for GameState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".to_string(),
|
||||||
|
player_color: None,
|
||||||
|
opponent_name: None,
|
||||||
|
match_id: None,
|
||||||
|
game_over: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UI state
|
||||||
|
enum AppState {
|
||||||
|
MainMenu,
|
||||||
|
Connecting,
|
||||||
|
FindingMatch,
|
||||||
|
InGame,
|
||||||
|
GameOver,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ChessApp {
|
||||||
|
state: AppState,
|
||||||
|
game_state: Arc<Mutex<GameState>>,
|
||||||
|
server_port: String,
|
||||||
|
username: String,
|
||||||
|
|
||||||
|
// Channels for communication with network tasks
|
||||||
|
tx_to_network: Option<mpsc::UnboundedSender<ClientEvent>>,
|
||||||
|
rx_from_network: Option<mpsc::UnboundedReceiver<ServerMessage2>>,
|
||||||
|
|
||||||
|
// UI state
|
||||||
|
selected_square: Option<(usize, usize)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ChessApp {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
state: AppState::MainMenu,
|
||||||
|
game_state: Arc::new(Mutex::new(GameState::default())),
|
||||||
|
server_port: "9001".to_string(),
|
||||||
|
username: "Player".to_string(),
|
||||||
|
tx_to_network: None,
|
||||||
|
rx_from_network: None,
|
||||||
|
selected_square: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChessApp {
|
||||||
|
fn connect_to_server(&mut self) {
|
||||||
|
let server_port = self.server_port.clone();
|
||||||
|
let username = self.username.clone();
|
||||||
|
let game_state = self.game_state.clone();
|
||||||
|
|
||||||
|
// Create channels for communication
|
||||||
|
let (tx_to_network, rx_from_ui) = mpsc::unbounded_channel();
|
||||||
|
let (tx_to_ui, rx_from_network) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
|
self.tx_to_network = Some(tx_to_network);
|
||||||
|
self.rx_from_network = Some(rx_from_network);
|
||||||
|
|
||||||
|
self.state = AppState::Connecting;
|
||||||
|
|
||||||
|
// Spawn network connection task
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if let Err(e) =
|
||||||
|
Self::network_handler(server_port, username, rx_from_ui, tx_to_ui, game_state).await
|
||||||
|
{
|
||||||
|
error!("Network handler error: {}", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn network_handler(
|
||||||
|
server_port: String,
|
||||||
|
username: String,
|
||||||
|
mut rx_from_ui: mpsc::UnboundedReceiver<ClientEvent>,
|
||||||
|
tx_to_ui: mpsc::UnboundedSender<ServerMessage2>,
|
||||||
|
game_state: Arc<Mutex<GameState>>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
// Build WebSocket URL
|
||||||
|
let server_address = format!("ws://127.0.0.1:{}", server_port);
|
||||||
|
let url = Url::parse(&server_address)?;
|
||||||
|
|
||||||
|
info!("Connecting to: {}", server_address);
|
||||||
|
let (ws_stream, _) = connect_async(url).await?;
|
||||||
|
let (mut write, mut read) = ws_stream.split();
|
||||||
|
|
||||||
|
// Send initial join message and immediately send FindMatch
|
||||||
|
let join_event = ClientEvent::Join { username };
|
||||||
|
write
|
||||||
|
.send(Message::Text(serde_json::to_string(&join_event)?))
|
||||||
|
.await?;
|
||||||
|
info!("Sent Join event");
|
||||||
|
|
||||||
|
// Send FindMatch immediately after joining
|
||||||
|
let find_match_event = ClientEvent::FindMatch;
|
||||||
|
write
|
||||||
|
.send(Message::Text(serde_json::to_string(&find_match_event)?))
|
||||||
|
.await?;
|
||||||
|
info!("Sent FindMatch event");
|
||||||
|
|
||||||
|
// Spawn reader task
|
||||||
|
let tx_to_ui_clone = tx_to_ui.clone();
|
||||||
|
let game_state_clone = game_state.clone();
|
||||||
|
let reader_handle = tokio::spawn(async move {
|
||||||
|
while let Some(message) = read.next().await {
|
||||||
|
match message {
|
||||||
|
Ok(msg) if msg.is_text() => {
|
||||||
|
let text = msg.to_text().unwrap();
|
||||||
|
info!("Received: {}", text);
|
||||||
|
|
||||||
|
if let Ok(server_msg) = serde_json::from_str::<ServerMessage2>(text) {
|
||||||
|
// Update game state
|
||||||
|
if let Ok(mut state) = game_state_clone.lock() {
|
||||||
|
match &server_msg {
|
||||||
|
ServerMessage2::UIUpdate { fen } => {
|
||||||
|
state.fen = fen.clone();
|
||||||
|
}
|
||||||
|
ServerMessage2::MatchFound {
|
||||||
|
color,
|
||||||
|
opponent_name,
|
||||||
|
match_id,
|
||||||
|
} => {
|
||||||
|
state.player_color = Some(color.clone());
|
||||||
|
state.opponent_name = Some(opponent_name.clone());
|
||||||
|
state.match_id = Some(match_id.clone());
|
||||||
|
}
|
||||||
|
ServerMessage2::GameEnd { winner } => {
|
||||||
|
state.game_over = Some(winner.clone());
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send to UI
|
||||||
|
if let Err(e) = tx_to_ui_clone.send(server_msg) {
|
||||||
|
error!("Failed to send to UI: {}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("WebSocket error: {}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Writer task (main thread)
|
||||||
|
while let Some(event) = rx_from_ui.recv().await {
|
||||||
|
let message = serde_json::to_string(&event)?;
|
||||||
|
write.send(Message::Text(message)).await?;
|
||||||
|
info!("Sent event to server: {:?}", event);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for reader to finish
|
||||||
|
let _ = reader_handle.await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_click(&mut self, row: usize, col: usize) {
|
||||||
|
if let Some((from_row, from_col)) = self.selected_square {
|
||||||
|
// Send move to server
|
||||||
|
if let Some(tx) = &self.tx_to_network {
|
||||||
|
// TODO: kinyerni a tenyleges kivalasztott babut
|
||||||
|
let chess_move = ChessMove::Quiet {
|
||||||
|
piece_type: engine::piecetype::PieceType::WhiteKing,
|
||||||
|
from_square: BoardSquare { x: 0, y: 1 },
|
||||||
|
to_square: BoardSquare { x: 2, y: 2 },
|
||||||
|
promotion_piece: None,
|
||||||
|
};
|
||||||
|
let move_event = ClientEvent::Move { step: chess_move };
|
||||||
|
let _ = tx.send(move_event);
|
||||||
|
}
|
||||||
|
self.selected_square = None;
|
||||||
|
} else {
|
||||||
|
// Select square
|
||||||
|
self.selected_square = Some((row, col));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fen_to_board(&self, fen: &str) -> [[char; 8]; 8] {
|
||||||
|
let mut board = [[' '; 8]; 8];
|
||||||
|
let parts: Vec<&str> = fen.split_whitespace().collect();
|
||||||
|
let board_str = parts[0];
|
||||||
|
|
||||||
|
let mut row = 0;
|
||||||
|
let mut col = 0;
|
||||||
|
|
||||||
|
for c in board_str.chars() {
|
||||||
|
if c == '/' {
|
||||||
|
row += 1;
|
||||||
|
col = 0;
|
||||||
|
} else if c.is_digit(10) {
|
||||||
|
col += c.to_digit(10).unwrap() as usize;
|
||||||
|
} else {
|
||||||
|
if row < 8 && col < 8 {
|
||||||
|
board[row][col] = c;
|
||||||
|
}
|
||||||
|
col += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
board
|
||||||
|
}
|
||||||
|
|
||||||
|
fn chess_char_to_piece(&self, c: char) -> &'static str {
|
||||||
|
match c {
|
||||||
|
'K' => "♚",
|
||||||
|
'Q' => "♛",
|
||||||
|
'R' => "♜",
|
||||||
|
'B' => "♝",
|
||||||
|
'N' => "♞",
|
||||||
|
'P' => "♟︎",
|
||||||
|
'k' => "♚",
|
||||||
|
'q' => "♛",
|
||||||
|
'r' => "♜",
|
||||||
|
'b' => "♝",
|
||||||
|
'n' => "♞",
|
||||||
|
'p' => "♟︎",
|
||||||
|
_ => "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_network_messages(&mut self) {
|
||||||
|
if let Some(rx) = &mut self.rx_from_network {
|
||||||
|
while let Ok(msg) = rx.try_recv() {
|
||||||
|
match msg {
|
||||||
|
ServerMessage2::MatchFound { .. } => {
|
||||||
|
info!("Match found! Transitioning to InGame state");
|
||||||
|
self.state = AppState::InGame;
|
||||||
|
}
|
||||||
|
ServerMessage2::GameEnd { .. } => {
|
||||||
|
info!("Game over! Transitioning to GameOver state");
|
||||||
|
self.state = AppState::GameOver;
|
||||||
|
}
|
||||||
|
ServerMessage2::Ok { response } => {
|
||||||
|
info!("Server OK response: {:?}", response);
|
||||||
|
// When we get the OK response, transition to FindingMatch state
|
||||||
|
// This shows the "Finding Match..." screen while we wait
|
||||||
|
if matches!(self.state, AppState::Connecting) {
|
||||||
|
self.state = AppState::FindingMatch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ServerMessage2::UIUpdate { fen } => {
|
||||||
|
info!("Board updated with FEN: {}", fen);
|
||||||
|
// UI will automatically redraw with new FEN
|
||||||
|
if let Ok(mut game_state) = self.game_state.lock() {
|
||||||
|
game_state.fen = fen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eframe::App for ChessApp {
|
||||||
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||||
|
// Process incoming network messages
|
||||||
|
self.process_network_messages();
|
||||||
|
|
||||||
|
// Get current game state
|
||||||
|
let game_state = self.game_state.lock().unwrap().clone();
|
||||||
|
|
||||||
|
match self.state {
|
||||||
|
AppState::MainMenu => {
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.vertical_centered(|ui| {
|
||||||
|
ui.heading("♞ Knightly ♞");
|
||||||
|
ui.add_space(30.0);
|
||||||
|
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
ui.label("Username:");
|
||||||
|
ui.text_edit_singleline(&mut self.username);
|
||||||
|
});
|
||||||
|
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
ui.label("Server Port:");
|
||||||
|
ui.text_edit_singleline(&mut self.server_port);
|
||||||
|
});
|
||||||
|
|
||||||
|
ui.add_space(20.0);
|
||||||
|
|
||||||
|
if ui.button("Connect & Play").clicked() {
|
||||||
|
self.connect_to_server();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ui.button("Quit").clicked() {
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
AppState::Connecting => {
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
// TODO: go back to menu btn
|
||||||
|
ui.vertical_centered(|ui| {
|
||||||
|
ui.heading("Connecting to Server...");
|
||||||
|
ui.add_space(20.0);
|
||||||
|
ui.spinner();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
AppState::FindingMatch => {
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
// TODO: go back to menu btn
|
||||||
|
// TODO: how do we delete the player from the waiting queue
|
||||||
|
ui.vertical_centered(|ui| {
|
||||||
|
ui.heading("Finding Match...");
|
||||||
|
ui.add_space(20.0);
|
||||||
|
ui.label("Waiting for an opponent...");
|
||||||
|
ui.spinner();
|
||||||
|
|
||||||
|
ui.add_space(20.0);
|
||||||
|
if ui.button("cancel").clicked() {
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
AppState::InGame => {
|
||||||
|
// Draw menu bar
|
||||||
|
egui::TopBottomPanel::top("menu_bar").show(ctx, |ui| {
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
if ui.button("Main Menu").clicked() {
|
||||||
|
*self = ChessApp::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ui.button("Resign").clicked() {
|
||||||
|
if let Some(tx) = &self.tx_to_network {
|
||||||
|
let _ = tx.send(ClientEvent::Resign);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.separator();
|
||||||
|
|
||||||
|
if let Some(color) = &game_state.player_color {
|
||||||
|
ui.label(format!("You are: {}", color));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(opponent) = &game_state.opponent_name {
|
||||||
|
ui.label(format!("vs: {}", opponent));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Draw chess board
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.vertical_centered(|ui| {
|
||||||
|
let board = self.fen_to_board(&game_state.fen);
|
||||||
|
let is_white = game_state
|
||||||
|
.player_color
|
||||||
|
.as_ref()
|
||||||
|
.map_or(true, |c| c == "white");
|
||||||
|
|
||||||
|
let available_size = ui.available_size();
|
||||||
|
let board_size = available_size.x.min(available_size.y) * 0.9;
|
||||||
|
let tile_size = board_size / 8.0;
|
||||||
|
|
||||||
|
let (response, painter) = ui.allocate_painter(
|
||||||
|
egui::Vec2::new(board_size, board_size),
|
||||||
|
egui::Sense::click(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let board_top_left = response.rect.left_top();
|
||||||
|
|
||||||
|
// Draw board and pieces
|
||||||
|
for row in 0..8 {
|
||||||
|
for col in 0..8 {
|
||||||
|
let (display_row, display_col) = if is_white {
|
||||||
|
(row, col)
|
||||||
|
} else {
|
||||||
|
(7 - row, 7 - col)
|
||||||
|
};
|
||||||
|
|
||||||
|
let color = if (row + col) % 2 == 0 {
|
||||||
|
egui::Color32::from_rgb(240, 217, 181) // Light
|
||||||
|
} else {
|
||||||
|
egui::Color32::from_rgb(181, 136, 99) // Dark
|
||||||
|
};
|
||||||
|
|
||||||
|
let rect = egui::Rect::from_min_size(
|
||||||
|
egui::Pos2::new(
|
||||||
|
board_top_left.x + col as f32 * tile_size,
|
||||||
|
board_top_left.y + row as f32 * tile_size,
|
||||||
|
),
|
||||||
|
egui::Vec2::new(tile_size, tile_size),
|
||||||
|
);
|
||||||
|
|
||||||
|
painter.rect_filled(rect, 0.0, color);
|
||||||
|
|
||||||
|
// Draw piece
|
||||||
|
let piece_char = board[display_row][display_col];
|
||||||
|
if piece_char != ' ' {
|
||||||
|
let symbol = self.chess_char_to_piece(piece_char);
|
||||||
|
let font_id = egui::FontId::proportional(tile_size * 0.8);
|
||||||
|
let text_color = if piece_char.is_uppercase() {
|
||||||
|
egui::Color32::WHITE
|
||||||
|
} else {
|
||||||
|
egui::Color32::BLACK
|
||||||
|
};
|
||||||
|
|
||||||
|
painter.text(
|
||||||
|
rect.center(),
|
||||||
|
egui::Align2::CENTER_CENTER,
|
||||||
|
symbol,
|
||||||
|
font_id,
|
||||||
|
text_color,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw selection
|
||||||
|
if let Some((sel_row, sel_col)) = self.selected_square {
|
||||||
|
if sel_row == display_row && sel_col == display_col {
|
||||||
|
painter.rect_stroke(
|
||||||
|
rect,
|
||||||
|
0.0,
|
||||||
|
egui::Stroke::new(3.0, egui::Color32::RED),
|
||||||
|
egui::StrokeKind::Middle,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle clicks
|
||||||
|
if response.clicked() {
|
||||||
|
if let Some(click_pos) = ui.ctx().pointer_interact_pos() {
|
||||||
|
if rect.contains(click_pos) {
|
||||||
|
self.handle_click(display_row, display_col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
AppState::GameOver => {
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.vertical_centered(|ui| {
|
||||||
|
ui.heading("Game Over");
|
||||||
|
ui.add_space(20.0);
|
||||||
|
|
||||||
|
if let Some(reason) = &game_state.game_over {
|
||||||
|
ui.label(format!("Result: {}", reason));
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.add_space(20.0);
|
||||||
|
|
||||||
|
if ui.button("Back to Main Menu").clicked() {
|
||||||
|
*self = ChessApp::default();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request repaint to keep UI responsive
|
||||||
|
ctx.request_repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_default_game_state() {
|
||||||
|
let game_state = GameState::default();
|
||||||
|
assert_eq!(
|
||||||
|
game_state.fen,
|
||||||
|
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||||
|
);
|
||||||
|
assert_eq!(game_state.player_color, None);
|
||||||
|
assert_eq!(game_state.opponent_name, None);
|
||||||
|
assert_eq!(game_state.match_id, None);
|
||||||
|
assert_eq!(game_state.game_over, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_fen_to_board_starting_position() {
|
||||||
|
let app = ChessApp::default();
|
||||||
|
let board = app.fen_to_board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR");
|
||||||
|
|
||||||
|
// Test black pieces on rank 0
|
||||||
|
assert_eq!(board[0][0], 'r');
|
||||||
|
assert_eq!(board[0][1], 'n');
|
||||||
|
assert_eq!(board[0][2], 'b');
|
||||||
|
assert_eq!(board[0][3], 'q');
|
||||||
|
assert_eq!(board[0][4], 'k');
|
||||||
|
assert_eq!(board[0][5], 'b');
|
||||||
|
assert_eq!(board[0][6], 'n');
|
||||||
|
assert_eq!(board[0][7], 'r');
|
||||||
|
|
||||||
|
// Test black pawns on rank 1
|
||||||
|
for col in 0..8 {
|
||||||
|
assert_eq!(board[1][col], 'p');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test empty squares in the middle
|
||||||
|
for row in 2..6 {
|
||||||
|
for col in 0..8 {
|
||||||
|
assert_eq!(board[row][col], ' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test white pawns on rank 6
|
||||||
|
for col in 0..8 {
|
||||||
|
assert_eq!(board[6][col], 'P');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test white pieces on rank 7
|
||||||
|
assert_eq!(board[7][0], 'R');
|
||||||
|
assert_eq!(board[7][1], 'N');
|
||||||
|
assert_eq!(board[7][2], 'B');
|
||||||
|
assert_eq!(board[7][3], 'Q');
|
||||||
|
assert_eq!(board[7][4], 'K');
|
||||||
|
assert_eq!(board[7][5], 'B');
|
||||||
|
assert_eq!(board[7][6], 'N');
|
||||||
|
assert_eq!(board[7][7], 'R');
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_fen_to_board_with_numbers() {
|
||||||
|
let app = ChessApp::default();
|
||||||
|
let board = app.fen_to_board("4k3/8/8/8/8/8/8/4K3");
|
||||||
|
|
||||||
|
// Test empty squares around kings
|
||||||
|
for row in 0..8 {
|
||||||
|
for col in 0..8 {
|
||||||
|
if (row == 0 && col == 4) || (row == 7 && col == 4) {
|
||||||
|
continue; // Skip king positions
|
||||||
|
}
|
||||||
|
assert_eq!(board[row][col], ' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test king positions
|
||||||
|
assert_eq!(board[0][4], 'k'); // black king
|
||||||
|
assert_eq!(board[7][4], 'K'); // white king
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_chess_char_to_piece() {
|
||||||
|
let app = ChessApp::default();
|
||||||
|
|
||||||
|
// Test white pieces
|
||||||
|
assert_eq!(app.chess_char_to_piece('K'), "♚");
|
||||||
|
assert_eq!(app.chess_char_to_piece('Q'), "♛");
|
||||||
|
assert_eq!(app.chess_char_to_piece('R'), "♜");
|
||||||
|
assert_eq!(app.chess_char_to_piece('B'), "♝");
|
||||||
|
assert_eq!(app.chess_char_to_piece('N'), "♞");
|
||||||
|
assert_eq!(app.chess_char_to_piece('P'), "♟︎");
|
||||||
|
|
||||||
|
// Test black pieces
|
||||||
|
assert_eq!(app.chess_char_to_piece('k'), "♚");
|
||||||
|
assert_eq!(app.chess_char_to_piece('q'), "♛");
|
||||||
|
assert_eq!(app.chess_char_to_piece('r'), "♜");
|
||||||
|
assert_eq!(app.chess_char_to_piece('b'), "♝");
|
||||||
|
assert_eq!(app.chess_char_to_piece('n'), "♞");
|
||||||
|
assert_eq!(app.chess_char_to_piece('p'), "♟︎");
|
||||||
|
|
||||||
|
// Test invalid piece
|
||||||
|
assert_eq!(app.chess_char_to_piece('X'), "");
|
||||||
|
assert_eq!(app.chess_char_to_piece(' '), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_chess_app_default() {
|
||||||
|
let app = ChessApp::default();
|
||||||
|
|
||||||
|
assert_eq!(app.server_port, "9001");
|
||||||
|
assert_eq!(app.username, "Player");
|
||||||
|
assert!(app.tx_to_network.is_none());
|
||||||
|
assert!(app.rx_from_network.is_none());
|
||||||
|
assert!(app.selected_square.is_none());
|
||||||
|
|
||||||
|
// Verify initial state is MainMenu
|
||||||
|
match app.state {
|
||||||
|
AppState::MainMenu => (),
|
||||||
|
_ => panic!("Expected initial state to be MainMenu"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_game_state_clone() {
|
||||||
|
let mut original = GameState::default();
|
||||||
|
original.player_color = Some("white".to_string());
|
||||||
|
original.opponent_name = Some("Opponent".to_string());
|
||||||
|
original.match_id = Some(Uuid::new_v4());
|
||||||
|
original.game_over = Some("Checkmate".to_string());
|
||||||
|
|
||||||
|
let cloned = original.clone();
|
||||||
|
|
||||||
|
assert_eq!(original.fen, cloned.fen);
|
||||||
|
assert_eq!(original.player_color, cloned.player_color);
|
||||||
|
assert_eq!(original.opponent_name, cloned.opponent_name);
|
||||||
|
assert_eq!(original.match_id, cloned.match_id);
|
||||||
|
assert_eq!(original.game_over, cloned.game_over);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_handle_click_selection() {
|
||||||
|
let mut app = ChessApp::default();
|
||||||
|
|
||||||
|
// Initially no square should be selected
|
||||||
|
assert_eq!(app.selected_square, None);
|
||||||
|
|
||||||
|
// Click on a square should select it
|
||||||
|
app.handle_click(3, 4);
|
||||||
|
assert_eq!(app.selected_square, Some((3, 4)));
|
||||||
|
|
||||||
|
// Click on another square should deselect and send move (if tx exists)
|
||||||
|
// Since we don't have a real tx in tests, we just verify the selection is cleared
|
||||||
|
app.handle_click(4, 4);
|
||||||
|
assert_eq!(app.selected_square, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_network_messages_match_found() {
|
||||||
|
let mut app = ChessApp::default();
|
||||||
|
let (tx, mut rx) = mpsc::unbounded_channel();
|
||||||
|
app.rx_from_network = Some(rx);
|
||||||
|
|
||||||
|
// Send a MatchFound message
|
||||||
|
let match_id = Uuid::new_v4();
|
||||||
|
let message = ServerMessage2::MatchFound {
|
||||||
|
match_id,
|
||||||
|
color: "white".to_string(),
|
||||||
|
opponent_name: "TestOpponent".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
tx.send(message).unwrap();
|
||||||
|
|
||||||
|
// Process the message
|
||||||
|
app.process_network_messages();
|
||||||
|
|
||||||
|
// State should transition to InGame
|
||||||
|
match app.state {
|
||||||
|
AppState::InGame => (),
|
||||||
|
_ => panic!("Expected state to transition to InGame"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_network_messages_game_over() {
|
||||||
|
let mut app = ChessApp::default();
|
||||||
|
let (tx, mut rx) = mpsc::unbounded_channel();
|
||||||
|
app.rx_from_network = Some(rx);
|
||||||
|
|
||||||
|
// Send a GameEnd message
|
||||||
|
let message = ServerMessage2::GameEnd {
|
||||||
|
winner: "White won by checkmate".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
tx.send(message).unwrap();
|
||||||
|
|
||||||
|
// Process the message
|
||||||
|
app.process_network_messages();
|
||||||
|
|
||||||
|
// State should transition to GameOver
|
||||||
|
match app.state {
|
||||||
|
AppState::GameOver => (),
|
||||||
|
_ => panic!("Expected state to transition to GameOver"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_network_messages_ui_update() {
|
||||||
|
let mut app = ChessApp::default();
|
||||||
|
let (tx, mut rx) = mpsc::unbounded_channel();
|
||||||
|
app.rx_from_network = Some(rx);
|
||||||
|
|
||||||
|
// Send a UIUpdate message
|
||||||
|
let new_fen = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1".to_string();
|
||||||
|
let message = ServerMessage2::UIUpdate {
|
||||||
|
fen: new_fen.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
tx.send(message).unwrap();
|
||||||
|
|
||||||
|
// Process the message
|
||||||
|
app.process_network_messages();
|
||||||
|
|
||||||
|
// Game state should be updated with new FEN
|
||||||
|
let game_state = app.game_state.lock().unwrap();
|
||||||
|
assert_eq!(game_state.fen, new_fen);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_network_messages_ok_response() {
|
||||||
|
let mut app = ChessApp::default();
|
||||||
|
app.state = AppState::Connecting;
|
||||||
|
let (tx, mut rx) = mpsc::unbounded_channel();
|
||||||
|
app.rx_from_network = Some(rx);
|
||||||
|
|
||||||
|
// Send an Ok message
|
||||||
|
let message = ServerMessage2::Ok { response: Ok(()) };
|
||||||
|
|
||||||
|
tx.send(message).unwrap();
|
||||||
|
|
||||||
|
// Process the message
|
||||||
|
app.process_network_messages();
|
||||||
|
|
||||||
|
// State should transition to FindingMatch when in Connecting state
|
||||||
|
match app.state {
|
||||||
|
AppState::FindingMatch => (),
|
||||||
|
_ => panic!("Expected state to transition to FindingMatch"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_fen_edge_cases() {
|
||||||
|
let app = ChessApp::default();
|
||||||
|
|
||||||
|
// Test empty board
|
||||||
|
let empty_board = app.fen_to_board("8/8/8/8/8/8/8/8");
|
||||||
|
for row in 0..8 {
|
||||||
|
for col in 0..8 {
|
||||||
|
assert_eq!(empty_board[row][col], ' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test FEN with multiple digit numbers
|
||||||
|
let board = app.fen_to_board("k7/8/8/8/8/8/8/7K");
|
||||||
|
assert_eq!(board[0][0], 'k');
|
||||||
|
assert_eq!(board[7][7], 'K');
|
||||||
|
|
||||||
|
// Test FEN with mixed pieces and numbers
|
||||||
|
let board = app.fen_to_board("r3k2r/8/8/8/8/8/8/R3K2R");
|
||||||
|
assert_eq!(board[0][0], 'r');
|
||||||
|
assert_eq!(board[0][4], 'k');
|
||||||
|
assert_eq!(board[0][7], 'r');
|
||||||
|
assert_eq!(board[7][0], 'R');
|
||||||
|
assert_eq!(board[7][4], 'K');
|
||||||
|
assert_eq!(board[7][7], 'R');
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_client_event_serialization() {
|
||||||
|
// Test Join event
|
||||||
|
let join_event = ClientEvent::Join {
|
||||||
|
username: "test".to_string(),
|
||||||
|
};
|
||||||
|
let serialized = serde_json::to_string(&join_event).unwrap();
|
||||||
|
assert!(serialized.contains("Join"));
|
||||||
|
assert!(serialized.contains("test"));
|
||||||
|
|
||||||
|
// Test FindMatch event
|
||||||
|
let find_match_event = ClientEvent::FindMatch;
|
||||||
|
let serialized = serde_json::to_string(&find_match_event).unwrap();
|
||||||
|
assert!(serialized.contains("FindMatch"));
|
||||||
|
|
||||||
|
// Test Move event
|
||||||
|
let chess_move = ChessMove::Quiet {
|
||||||
|
piece_type: engine::piecetype::PieceType::WhiteKing,
|
||||||
|
from_square: BoardSquare { x: 0, y: 1 },
|
||||||
|
to_square: BoardSquare { x: 2, y: 2 },
|
||||||
|
promotion_piece: None,
|
||||||
|
};
|
||||||
|
let move_event = ClientEvent::Move { step: chess_move };
|
||||||
|
let serialized = serde_json::to_string(&move_event).unwrap();
|
||||||
|
assert!(serialized.contains("Move"));
|
||||||
|
|
||||||
|
// Test Resign event
|
||||||
|
let resign_event = ClientEvent::Resign;
|
||||||
|
let serialized = serde_json::to_string(&resign_event).unwrap();
|
||||||
|
assert!(serialized.contains("Resign"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_server_message_deserialization() {
|
||||||
|
// Test Ok message
|
||||||
|
let ok_json = r#"{"Ok":{"response":{"Ok":null}}}"#;
|
||||||
|
let message: ServerMessage2 = serde_json::from_str(ok_json).unwrap();
|
||||||
|
match message {
|
||||||
|
ServerMessage2::Ok { response } => {
|
||||||
|
assert!(response.is_ok());
|
||||||
|
}
|
||||||
|
_ => panic!("Expected Ok message"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test MatchFound message
|
||||||
|
let match_found_json = r#"{"MatchFound":{"match_id":"12345678-1234-1234-1234-123456789012","color":"white","opponent_name":"Test"}}"#;
|
||||||
|
let message: ServerMessage2 = serde_json::from_str(match_found_json).unwrap();
|
||||||
|
match message {
|
||||||
|
ServerMessage2::MatchFound {
|
||||||
|
match_id,
|
||||||
|
color,
|
||||||
|
opponent_name,
|
||||||
|
} => {
|
||||||
|
assert_eq!(color, "white");
|
||||||
|
assert_eq!(opponent_name, "Test");
|
||||||
|
}
|
||||||
|
_ => panic!("Expected MatchFound message"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test UIUpdate message
|
||||||
|
let ui_update_json =
|
||||||
|
r#"{"UIUpdate":{"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"}}"#;
|
||||||
|
let message: ServerMessage2 = serde_json::from_str(ui_update_json).unwrap();
|
||||||
|
match message {
|
||||||
|
ServerMessage2::UIUpdate { fen } => {
|
||||||
|
assert!(fen.contains("rnbqkbnr"));
|
||||||
|
}
|
||||||
|
_ => panic!("Expected UIUpdate message"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user