Gerakan Langkah Bidak Catur_Nine Dengan Formasi 2 Garuda, 1 Kuda Pada Papan 9x9 Petak
GERAKAN LANGKAH BIDAK CATUR_NINE DENGAN FORMASI 2 GARUDA, 1 KUDA PADA PAPAN 9x9 PETAK
(Oleh: SR.Pakpahan,SST)
Sehingga kode keseluruhan akan menjadi seperti berikut ini:# === POSISI AWAL BIDAK ===starting_position = {"white_rook": [(0, 8), (8, 8)],"white_knight": [(1, 8)], # Hanya 1 Kuda"white_bishop": [(3, 8), (6, 8)], # Gajah: satu di kiri, satu di kanan"white_garuda": [(2, 8), (7, 8)], # Garuda: satu di kiri, satu di kanan"white_queen": [(5, 8)],"white_king": [(4, 8)], # Raja tetap di tengah"white_pawn": [(i, 7) for i in range(9)],"black_rook": [(0, 0), (8, 0)],"black_knight": [(1, 0)], # Hanya 1 Kuda"black_bishop": [(3, 0), (6, 0)], # Gajah: satu di kiri, satu di kanan"black_garuda": [(2, 0), (7, 0)], # Garuda: satu di kiri, satu di kanan"black_queen": [(5, 0)],"black_king": [(4, 0)], # Raja tetap di tengah"black_pawn": [(i, 1) for i in range(9)],}
Kode di __main__.py
import pygame
import sys
import os
import time
from catur_nine_move import initialize_board, move_piece, is_valid_move, get_piece_at
pygame.init()
# Ukuran layar
SCREEN_WIDTH, SCREEN_HEIGHT = 900, 700
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Chess Nine with Timer")
# Ukuran papan catur
BOARD_SIZE = 635
TILE_SIZE = BOARD_SIZE // 9
BOARD_X = (SCREEN_WIDTH - BOARD_SIZE) // 2 - 90
BOARD_Y = (SCREEN_HEIGHT - BOARD_SIZE) // 2 - (-170)
# Warna
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
LIGHT_BROWN = (240, 217, 181)
DARK_BROWN = (181, 136, 99)
# Font
font = pygame.font.Font(None, 40)
# Direktori gambar bidak
IMAGE_DIR = "/storage/emulated/0/AppJadi/Catur/Catur_9x9/images"
# Muat gambar bidak
pieces_images = {}
def load_pieces():
piece_names = ["pawn", "rook", "knight", "bishop", "queen", "king", "garuda"]
colors = ["white", "black"]
for color in colors:
for piece in piece_names:
path = os.path.join(IMAGE_DIR, f"{color}_{piece}.png")
if os.path.exists(path):
img = pygame.image.load(path)
img = pygame.transform.scale(img, (TILE_SIZE, TILE_SIZE))
pieces_images[f"{color}_{piece}"] = img
# **Inisialisasi status papan**
board_state = initialize_board()
current_player = "white"
selected_piece = None
selected_position = None
def draw_board():
"""Menggambar papan catur"""
for row in range(9):
for col in range(9):
color = DARK_BROWN if (row + col) % 2 == 0 else LIGHT_BROWN
pygame.draw.rect(screen, color, (BOARD_X + col * TILE_SIZE, BOARD_Y + row * TILE_SIZE, TILE_SIZE, TILE_SIZE))
def draw_pieces():
"""Menggambar semua bidak di posisi yang benar"""
for color, pieces in board_state.items():
for piece, positions in pieces.items():
for pos in positions:
x, y = pos
screen.blit(pieces_images[f"{color}_{piece}"],
(BOARD_X + x * TILE_SIZE, BOARD_Y + y * TILE_SIZE))
def switch_turn():
"""Mengganti giliran pemain"""
global current_player
current_player = "black" if current_player == "white" else "white"
def handle_click(x, y):
"""Menangani klik untuk memilih dan memindahkan bidak"""
global selected_piece, selected_position, board_state, current_player
col = (x - BOARD_X) // TILE_SIZE
row = (y - BOARD_Y) // TILE_SIZE
if not (0 <= col < 9 and 0 <= row < 9):
return # Klik di luar papan
clicked_piece = get_piece_at(col, row, board_state)
if selected_piece:
# Coba gerakkan bidak ke lokasi baru
if is_valid_move(selected_piece, selected_position, (col, row), board_state, current_player):
move_piece(selected_piece, selected_position, (col, row), board_state)
switch_turn() # Ganti giliran setelah langkah sah
selected_piece = None
elif clicked_piece:
# Pilih bidak jika bidak milik pemain saat ini
if clicked_piece.startswith(current_player):
selected_piece = clicked_piece
selected_position = (col, row)
def main():
global board_state
clock = pygame.time.Clock()
board_state = initialize_board()
running = True
while running:
screen.fill(WHITE)
draw_board()
draw_pieces()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
handle_click(*pygame.mouse.get_pos())
pygame.display.flip()
clock.tick(30)
if __name__ == "__main__":
load_pieces()
main()
Kode di catur_nine_move.py
Tambahkan fungsi berikut ke dalam catur_nine_move.py
:
def initialize_board():
"""Mengatur posisi awal bidak pada papan Catur Nine 9x9"""
return {
"white": {
"rook": [(0, 8), (8, 8)],
"knight": [(1, 8)],
"bishop": [(3, 8), (6, 8)],
"queen": [(5, 8)],
"king": [(4, 8)],
"garuda": [(2, 8), (7, 8)],
"pawn": [(x, 7) for x in range(9)]
},
"black": {
"rook": [(0, 0), (8, 0)],
"knight": [(1, 0)],
"bishop": [(3, 0), (6, 0)],
"queen": [(5, 0)],
"king": [(4, 0)],
"garuda": [(2, 0), (7, 0)],
"pawn": [(x, 1) for x in range(9)]
}
}
def move_piece(piece, start, end, board_state):
"""Memindahkan bidak jika langkah valid"""
x1, y1 = start
x2, y2 = end
# Hapus dari posisi lama
for p_type, positions in board_state[piece.split("_")[0]].items():
if start in positions:
positions.remove(start)
break
# Tambahkan ke posisi baru
board_state[piece.split("_")[0]][piece.split("_")[1]].append(end)
def get_piece_at(x, y, board_state):
"""Mengembalikan nama bidak di posisi tertentu"""
for color, pieces in board_state.items():
for piece, positions in pieces.items():
if (x, y) in positions:
return f"{color}_{piece}"
return None
def is_valid_move(piece, start, end, board_state, turn):
"""Memeriksa apakah langkah sah"""
x1, y1 = start
x2, y2 = end
if get_piece_at(x2, y2, board_state) and get_piece_at(x2, y2, board_state).startswith(turn):
return False # Tidak bisa bergerak ke tempat yang ditempati bidak sendiri
piece_type = piece.split("_")[1]
if piece_type == "pawn":
direction = -1 if turn == "white" else 1
if x1 == x2 and y2 - y1 == direction:
return True
if abs(x1 - x2) == 1 and y2 - y1 == direction:
return get_piece_at(x2, y2, board_state) is not None
elif piece_type == "rook":
return x1 == x2 or y1 == y2
elif piece_type == "knight":
return (abs(x2 - x1), abs(y2 - y1)) in [(2, 1), (1, 2)]
elif piece_type == "bishop":
return abs(x2 - x1) == abs(y2 - y1)
elif piece_type == "queen":
return abs(x2 - x1) == abs(y2 - y1) or x1 == x2 or y1 == y2
elif piece_type == "king":
return abs(x2 - x1) <= 1 and abs(y2 - y1) <= 1
elif piece_type == "garuda":
return (abs(x2 - x1) == 2 and y1 == y2) or (abs(y2 - y1) == 2 and x1 == x2) or (abs(x2 - x1) == 2 and abs(y2 - y1) == 2)
return False
Coba jalankan kode ini, bidak catur_nine akan dapat digerakkan, dan selanjutnya kode disesuaikan dengan fungsi yang digunakan.
Kode Lengkap Catur_Nine
Kode Lengkap Isi file Catur_Nine formasi 2 Garuda, 1 Kuda dapat di download di bawah ini dengan cara mengikuti syarat/ketentuan berikut:
1. Kirimkan Pulsa Rp 14000 ke nomor 082170814310
2. Verifikasi kepada nomor HP saya tersebut bahwa Anda telah mengirim Pulsa.
3. Saya akan memberikan kunci Password bagi Anda untuk dapat mendownload file Catur Nine
4. File tersebut buka di Pydroid3 atau di teks editor lainnya, lalu jalankan.
5. Dan selamat bermain Catur Nine 9x9 petak.
Comments
Post a Comment