langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
145
Task/Minesweeper-game/OCaml/minesweeper-game.ocaml
Normal file
145
Task/Minesweeper-game/OCaml/minesweeper-game.ocaml
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
exception Lost
|
||||
exception Won
|
||||
|
||||
let put_mines g m n mines_number =
|
||||
let rec aux i =
|
||||
if i < mines_number then
|
||||
begin
|
||||
let x = Random.int n
|
||||
and y = Random.int m in
|
||||
if g.(y).(x)
|
||||
then aux i
|
||||
else begin
|
||||
g.(y).(x) <- true;
|
||||
aux (succ i)
|
||||
end
|
||||
end
|
||||
in
|
||||
aux 0
|
||||
|
||||
let print_abscissas n =
|
||||
print_string "\n "; for x = 1 to n do print_int (x mod 10) done;
|
||||
print_string "\n "; for x = 1 to n do print_char '|' done;
|
||||
print_newline()
|
||||
|
||||
let print_display d n =
|
||||
print_abscissas n;
|
||||
Array.iteri (fun y line ->
|
||||
Printf.printf " %2d - " (y+1); (* print ordinates *)
|
||||
Array.iter print_char line;
|
||||
print_newline()
|
||||
) d;
|
||||
print_newline()
|
||||
|
||||
let reveal d g n =
|
||||
print_abscissas n;
|
||||
Array.iteri (fun y line ->
|
||||
Printf.printf " %2d - " (y+1); (* print ordinates *)
|
||||
Array.iteri (fun x c ->
|
||||
print_char (
|
||||
match c, g.(y).(x) with
|
||||
| '0'..'9', _ -> c
|
||||
| '.', true -> 'x'
|
||||
| '?', true -> 'X'
|
||||
| '?', false -> 'N'
|
||||
| '.', false -> '.'
|
||||
| _ -> c)
|
||||
) line;
|
||||
print_newline()
|
||||
) d;
|
||||
print_newline()
|
||||
|
||||
let toggle_mark d x y =
|
||||
match d.(y).(x) with
|
||||
| '.' -> d.(y).(x) <- '?'
|
||||
| '?' -> d.(y).(x) <- '.'
|
||||
| _ -> ()
|
||||
|
||||
let rec feedback g d x y =
|
||||
if d.(y).(x) = '.' then
|
||||
begin
|
||||
let n = ref 0 in (* the number of mines around *)
|
||||
for i = (pred y) to (succ y) do
|
||||
for j = (pred x) to (succ x) do
|
||||
try if g.(i).(j) then incr n
|
||||
with _ -> ()
|
||||
done;
|
||||
done;
|
||||
match !n with
|
||||
| 0 ->
|
||||
(* recursive feedback when no mines are around *)
|
||||
d.(y).(x) <- ' ';
|
||||
for j = (pred y) to (succ y) do
|
||||
for i = (pred x) to (succ x) do
|
||||
try feedback g d i j
|
||||
with _ -> ()
|
||||
done
|
||||
done
|
||||
| _ ->
|
||||
d.(y).(x) <- (string_of_int !n).[0]
|
||||
end
|
||||
|
||||
let clear_cell g d x y =
|
||||
if g.(y).(x)
|
||||
then (d.(y).(x) <- '!'; raise Lost)
|
||||
else feedback g d x y
|
||||
|
||||
let rec user_input g d =
|
||||
try
|
||||
let s = read_line() in
|
||||
match Scanf.sscanf s "%c %d %d" (fun c x y -> c,x,y) with
|
||||
| 'm', x, y -> toggle_mark d (x-1) (y-1)
|
||||
| 'c', x, y -> clear_cell g d (x-1) (y-1)
|
||||
| _ -> raise Exit
|
||||
with Exit | Scanf.Scan_failure _
|
||||
| Invalid_argument "index out of bounds" ->
|
||||
print_string "# wrong input, try again\n> ";
|
||||
user_input g d
|
||||
|
||||
let check_won g d =
|
||||
let won = ref true in
|
||||
Array.iteri (fun y line ->
|
||||
Array.iteri (fun x c ->
|
||||
match c, g.(y).(x) with
|
||||
| '.', _ -> won := false
|
||||
| '?', false -> won := false
|
||||
| _ -> ()
|
||||
) line
|
||||
) d;
|
||||
if !won then raise Won
|
||||
|
||||
let minesweeper n m percent =
|
||||
let round x = int_of_float (floor (x +. 0.5)) in
|
||||
let mines_number = round ((float (n * m)) *. percent) in
|
||||
(* the ground containing the mines *)
|
||||
let g = Array.make_matrix m n false in
|
||||
put_mines g m n mines_number;
|
||||
Printf.printf "# You have to find %d mines\n" mines_number;
|
||||
(* what's displayed to the user *)
|
||||
let d = Array.make_matrix m n '.' in
|
||||
try
|
||||
while true do
|
||||
print_display d n;
|
||||
print_string "> ";
|
||||
user_input g d;
|
||||
check_won g d;
|
||||
done
|
||||
with
|
||||
| Lost ->
|
||||
print_endline "# You lost!";
|
||||
reveal d g n
|
||||
| Won ->
|
||||
print_endline "# You won!";
|
||||
reveal d g n
|
||||
|
||||
let () =
|
||||
Random.self_init();
|
||||
let ios, fos = int_of_string, float_of_string in
|
||||
let n, m, percent =
|
||||
try ios Sys.argv.(1), ios Sys.argv.(2), fos Sys.argv.(3)
|
||||
with _ ->
|
||||
try ios Sys.argv.(1), ios Sys.argv.(2), 0.2
|
||||
with _ -> (6, 4, 0.2)
|
||||
in
|
||||
minesweeper n m percent;
|
||||
;;
|
||||
103
Task/Minesweeper-game/Perl-6/minesweeper-game.pl6
Normal file
103
Task/Minesweeper-game/Perl-6/minesweeper-game.pl6
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
enum Tile-Type <Empty Mine>;
|
||||
|
||||
class Tile {
|
||||
has Tile-Type $.type;
|
||||
has $.face is rw;
|
||||
method Str { $.face // '.' }
|
||||
}
|
||||
|
||||
class Field {
|
||||
has Tile @!grid;
|
||||
has Int $!width;
|
||||
has Int $!height;
|
||||
has Int $!mine-spots is rw;
|
||||
has Int $!empty-spots is rw;
|
||||
|
||||
method new (Int $width, Int $height, Num $mines-ratio=0.1) {
|
||||
|
||||
my $mine-spots = $width*$height*$mines-ratio;
|
||||
my $empty-spots = $width*$height - $mine-spots;
|
||||
|
||||
my @grid;
|
||||
for ^$width X ^$height -> $i, $j {
|
||||
@grid[$i][$j] = Tile.new(type => Empty);
|
||||
}
|
||||
for (^$width).pick($mine-spots) Z (^$height).pick($mine-spots) -> $i, $j {
|
||||
@grid[$i][$j] = Tile.new( type => Mine);
|
||||
}
|
||||
self.bless(*, :$width, :$height, :@grid, :$mine-spots, :$empty-spots);
|
||||
}
|
||||
|
||||
method open( $i, $j) {
|
||||
return if @!grid[$i][$j].face.defined;
|
||||
|
||||
self.end-game("KaBoom") if @!grid[$i][$j].type ~~ Mine;
|
||||
|
||||
my @neighbors = gather take [$i+.[0],$j+.[1]]
|
||||
if 0 <= $i + .[0] < $!width && 0 <= $j + .[1] < $!height
|
||||
for [-1,-1],[+0,-1],[+1,-1],
|
||||
[-1,+0],( ),[+1,+0],
|
||||
[-1,+1],[+0,+1],[+1,+1];
|
||||
|
||||
my $mines = [+] @neighbors.map: { @!grid[.[0]][.[1]].type ~~ Mine };
|
||||
|
||||
$!empty-spots--;
|
||||
@!grid[$i][$j].face = $mines > 0 ?? $mines !! ' ';
|
||||
|
||||
if $mines == 0 {
|
||||
self.open(.[0], .[1]) for @neighbors;
|
||||
}
|
||||
self.end-game("You won") if $!empty-spots == 0;
|
||||
}
|
||||
|
||||
method end-game(Str $msg ) {
|
||||
for ^$!width X ^$!height -> $i, $j {
|
||||
@!grid[$i][$j].face = '*' if @!grid[$i][$j].type ~~ Mine
|
||||
}
|
||||
die $msg;
|
||||
}
|
||||
|
||||
method mark ( $i, $j) {
|
||||
if !@!grid[$i][$j].face.defined {
|
||||
@!grid[$i][$j].face = "⚐";
|
||||
$!mine-spots-- if @!grid[$i][$j].type ~~ Mine;
|
||||
}elsif !@!grid[$i][$j].face eq "⚐" {
|
||||
undefine @!grid[$i][$j].face;
|
||||
$!mine-spots++ if @!grid[$i][$j].type ~~ Mine;
|
||||
}
|
||||
self.end-game("You won") if $!mine-spots == 0;
|
||||
}
|
||||
|
||||
method Str {
|
||||
[~] '┌', '─' xx $!height, "┐\n",
|
||||
join '', do for ^$!width -> $i {
|
||||
'│', @!grid[$i][*], "│\n";
|
||||
}, '└', '─' xx $!height, '┘';
|
||||
}
|
||||
|
||||
method valid ($i, $j) {
|
||||
0 <= $i < $!width && 0 <= $j < $!height
|
||||
}
|
||||
}
|
||||
|
||||
my $f = Field.new(6,10);
|
||||
|
||||
loop {
|
||||
say $f;
|
||||
my ($c,$x,$y) = prompt("[open|mark] x y: ").split(/\s+/);
|
||||
try {
|
||||
given $c {
|
||||
when !$f.valid($y,$x) { say "invalid coordinates" }
|
||||
when 'open' { $f.open($y,$x) }
|
||||
when 'mark' { $f.mark($y,$x) }
|
||||
default {say "invalid cmd" }
|
||||
}
|
||||
CATCH {
|
||||
say "$!: end of game.";
|
||||
return;
|
||||
}
|
||||
}
|
||||
last if $!;
|
||||
}
|
||||
|
||||
say $f;
|
||||
178
Task/Minesweeper-game/PureBasic/minesweeper-game.purebasic
Normal file
178
Task/Minesweeper-game/PureBasic/minesweeper-game.purebasic
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
Structure cell
|
||||
isMine.i
|
||||
display.c ;character to displays for cell, one of these {'.', '?', ' ', #)
|
||||
EndStructure
|
||||
|
||||
Global Dim grid.cell(0,0)
|
||||
Global mineCount, minesMarked, isGameOver
|
||||
|
||||
Procedure makeGrid(n, m)
|
||||
Protected rm, x, y
|
||||
Dim grid.cell(n - 1, m - 1)
|
||||
mineCount = n * m * (Random(4) + 2) / 10
|
||||
If mineCount < 0: mineCount = 1: EndIf
|
||||
|
||||
For x = 0 To n - 1
|
||||
For y = 0 To m - 1
|
||||
grid(x, y)\display = '.'
|
||||
Next
|
||||
Next
|
||||
|
||||
rm = mineCount
|
||||
While rm
|
||||
x = Random(n - 1)
|
||||
y = Random(m - 1)
|
||||
If Not grid(x, y)\isMine
|
||||
rm - 1: grid(x, y)\isMine = #True
|
||||
EndIf
|
||||
Wend
|
||||
minesMarked = 0
|
||||
isGameOver = #False
|
||||
EndProcedure
|
||||
|
||||
Procedure displayGrid(isEndOfGame = #False)
|
||||
#lMargin = 4
|
||||
Protected x, y, display.s
|
||||
If Not isEndOfGame
|
||||
PrintN("Grid has " + Str(mineCount) + " mines, " + Str(minesMarked) + " mines marked.")
|
||||
EndIf
|
||||
PrintN(Space(#lMargin + 1) + ReplaceString(Space(ArraySize(grid(), 1) + 1), " ", "-"))
|
||||
For y = 0 To ArraySize(grid(), 2)
|
||||
Print(RSet(Str(y + 1), #lMargin, " ") + ":")
|
||||
For x = 0 To ArraySize(grid(), 1)
|
||||
Print(Chr(grid(x,y)\display))
|
||||
Next
|
||||
PrintN("")
|
||||
Next
|
||||
EndProcedure
|
||||
|
||||
Procedure endGame(msg.s)
|
||||
Protected ans.s
|
||||
isGameOver = #True
|
||||
PrintN(msg): Print("Another game (y/n)?"): ans = Input()
|
||||
If LCase(Left(Trim(ans),1)) = "y"
|
||||
makeGrid(6, 4)
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure resign()
|
||||
Protected x, y, found
|
||||
For y = 0 To ArraySize(grid(), 2)
|
||||
For x = 0 To ArraySize(grid(), 1)
|
||||
With grid(x,y)
|
||||
If \isMine
|
||||
If \display = '?'
|
||||
\display = 'Y'
|
||||
found + 1
|
||||
ElseIf \display <> 'x'
|
||||
\display = 'N'
|
||||
EndIf
|
||||
EndIf
|
||||
EndWith
|
||||
Next
|
||||
Next
|
||||
displayGrid(#True)
|
||||
endGame("You found " + Str(found) + " out of " + Str(mineCount) + " mines.")
|
||||
EndProcedure
|
||||
|
||||
Procedure usage()
|
||||
PrintN("h or ? - this help,")
|
||||
PrintN("c x y - clear cell (x,y),")
|
||||
PrintN("m x y - marks (toggles) cell (x,y),")
|
||||
PrintN("n - start a new game,")
|
||||
PrintN("q - quit/resign the game,")
|
||||
PrintN("where x is the (horizontal) column number and y is the (vertical) row number." + #CRLF$)
|
||||
EndProcedure
|
||||
|
||||
Procedure markCell(x, y)
|
||||
If grid(x, y)\display = '?'
|
||||
minesMarked - 1: grid(x, y)\display = '.'
|
||||
ElseIf grid(x, y)\display = '.'
|
||||
minesMarked + 1: grid(x, y)\display = '?'
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure countAdjMines(x, y)
|
||||
Protected count, i, j
|
||||
For j = y - 1 To y + 1
|
||||
If j >= 0 And j <= ArraySize(grid(), 2)
|
||||
For i = x - 1 To x + 1
|
||||
If i >= 0 And i <= ArraySize(grid(), 1)
|
||||
If grid(i, j)\isMine
|
||||
count + 1
|
||||
EndIf
|
||||
EndIf
|
||||
Next
|
||||
EndIf
|
||||
Next
|
||||
|
||||
ProcedureReturn count
|
||||
EndProcedure
|
||||
|
||||
Procedure clearCell(x, y)
|
||||
Protected count
|
||||
If x >= 0 And x <= ArraySize(grid(), 1) And y >= 0 And y <= ArraySize(grid(), 2)
|
||||
If grid(x, y)\display = '.'
|
||||
If Not grid(x,y)\isMine
|
||||
count = countAdjMines(x, y)
|
||||
If count
|
||||
grid(x, y)\display = Asc(Str(count))
|
||||
Else
|
||||
grid(x, y)\display = ' '
|
||||
clearCell(x + 1, y)
|
||||
clearCell(x + 1, y + 1)
|
||||
clearCell(x , y + 1)
|
||||
clearCell(x - 1, y + 1)
|
||||
clearCell(x - 1, y)
|
||||
clearCell(x - 1, y - 1)
|
||||
clearCell(x , y - 1)
|
||||
clearCell(x + 1, y - 1)
|
||||
EndIf
|
||||
Else
|
||||
grid(x, y)\display = 'x'
|
||||
PrintN("Kaboom! You lost!")
|
||||
resign()
|
||||
EndIf
|
||||
EndIf
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure testforwin()
|
||||
Protected x, y, isCleared
|
||||
If minesMarked = mineCount
|
||||
isCleared = #True
|
||||
For x = 0 To ArraySize(grid(), 1)
|
||||
For y = 0 To ArraySize(grid(), 2)
|
||||
If grid(x, y)\display = '.': isCleared = #False: EndIf
|
||||
Next
|
||||
Next
|
||||
EndIf
|
||||
If isCleared: endGame("You won!"): EndIf
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
Define action.s
|
||||
usage()
|
||||
makeGrid(6, 4): displayGrid()
|
||||
Repeat
|
||||
PrintN(""): Print(">"): action = Input()
|
||||
Select Asc(LCase(Left(action, 1)))
|
||||
Case 'h', '?'
|
||||
usage()
|
||||
Case 'n'
|
||||
makeGrid(6, 4): displayGrid()
|
||||
Case 'c'
|
||||
clearCell(Val(StringField(action, 2, " ")) - 1, Val(StringField(action, 3, " ")) - 1)
|
||||
If Not isGameOver: displayGrid(): Else: testforwin(): EndIf
|
||||
Case 'm'
|
||||
markCell(Val(StringField(action, 2, " ")) - 1, Val(StringField(action, 3, " ")) - 1)
|
||||
displayGrid()
|
||||
testforwin()
|
||||
Case 'q'
|
||||
resign()
|
||||
EndSelect
|
||||
Until isGameOver
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
Loading…
Add table
Add a link
Reference in a new issue