aephiday commited on
Commit
ea72b5a
·
verified ·
1 Parent(s): 658e353

Create minimal_global.R

Browse files
Files changed (1) hide show
  1. minimal_global.R +186 -0
minimal_global.R ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Minimal Global Functions for Puzzle8 Game
2
+ # Simple, fast, and focused on core gameplay
3
+
4
+ # Constants
5
+ SOLVED_STATE <- c(1, 2, 3, 4, 5, 6, 7, 8, 0)
6
+
7
+ # Utility functions
8
+ pos_to_coords <- function(pos) {
9
+ list(row = (pos - 1) %/% 3 + 1, col = (pos - 1) %% 3 + 1)
10
+ }
11
+
12
+ coords_to_pos <- function(row, col) {
13
+ (row - 1) * 3 + col
14
+ }
15
+
16
+ is_valid_position <- function(row, col) {
17
+ row >= 1 && row <= 3 && col >= 1 && col <= 3
18
+ }
19
+
20
+ # Create a solvable puzzle using simple shuffling
21
+ create_solvable_puzzle <- function(moves = 100) {
22
+ puzzle <- SOLVED_STATE
23
+ blank_pos <- 9
24
+
25
+ # Simple shuffling with valid moves
26
+ for(i in 1:moves) {
27
+ coords <- pos_to_coords(blank_pos)
28
+ valid_moves <- c()
29
+
30
+ # Check all four directions
31
+ directions <- list(
32
+ list(row = -1, col = 0), # Up
33
+ list(row = 1, col = 0), # Down
34
+ list(row = 0, col = -1), # Left
35
+ list(row = 0, col = 1) # Right
36
+ )
37
+
38
+ for(dir in directions) {
39
+ new_row <- coords$row + dir$row
40
+ new_col <- coords$col + dir$col
41
+
42
+ if(is_valid_position(new_row, new_col)) {
43
+ valid_moves <- c(valid_moves, coords_to_pos(new_row, new_col))
44
+ }
45
+ }
46
+
47
+ if(length(valid_moves) > 0) {
48
+ move_pos <- sample(valid_moves, 1)
49
+
50
+ # Swap tiles
51
+ temp <- puzzle[blank_pos]
52
+ puzzle[blank_pos] <- puzzle[move_pos]
53
+ puzzle[move_pos] <- temp
54
+
55
+ blank_pos <- move_pos
56
+ }
57
+ }
58
+
59
+ # Ensure puzzle is not already solved
60
+ if(identical(puzzle, SOLVED_STATE)) {
61
+ return(create_solvable_puzzle(moves + 20))
62
+ }
63
+
64
+ return(puzzle)
65
+ }
66
+
67
+ # Check if puzzle is solved
68
+ is_solved <- function(puzzle) {
69
+ identical(puzzle, SOLVED_STATE)
70
+ }
71
+
72
+ # Get blank position
73
+ get_blank_position <- function(puzzle) {
74
+ which(puzzle == 0)[1]
75
+ }
76
+
77
+ # Get valid moves for current blank position
78
+ get_valid_moves <- function(blank_pos) {
79
+ coords <- pos_to_coords(blank_pos)
80
+ valid_moves <- c()
81
+
82
+ directions <- list(
83
+ list(row = -1, col = 0), # Up
84
+ list(row = 1, col = 0), # Down
85
+ list(row = 0, col = -1), # Left
86
+ list(row = 0, col = 1) # Right
87
+ )
88
+
89
+ for(dir in directions) {
90
+ new_row <- coords$row + dir$row
91
+ new_col <- coords$col + dir$col
92
+
93
+ if(is_valid_position(new_row, new_col)) {
94
+ valid_moves <- c(valid_moves, coords_to_pos(new_row, new_col))
95
+ }
96
+ }
97
+
98
+ return(valid_moves)
99
+ }
100
+
101
+ # Move tile with validation - core game mechanic
102
+ move_tile <- function(puzzle, tile_position) {
103
+ blank_pos <- get_blank_position(puzzle)
104
+ valid_moves <- get_valid_moves(blank_pos)
105
+
106
+ if(tile_position %in% valid_moves) {
107
+ # Create new puzzle state
108
+ new_puzzle <- puzzle
109
+ new_puzzle[blank_pos] <- puzzle[tile_position]
110
+ new_puzzle[tile_position] <- 0
111
+
112
+ return(list(puzzle = new_puzzle, moved = TRUE))
113
+ }
114
+
115
+ return(list(puzzle = puzzle, moved = FALSE))
116
+ }
117
+
118
+ # Check if puzzle is solvable using inversion count
119
+ is_solvable <- function(puzzle) {
120
+ # Count inversions (excluding blank tile)
121
+ tiles <- puzzle[puzzle != 0]
122
+ inversions <- 0
123
+
124
+ for(i in 1:(length(tiles) - 1)) {
125
+ for(j in (i + 1):length(tiles)) {
126
+ if(tiles[i] > tiles[j]) {
127
+ inversions <- inversions + 1
128
+ }
129
+ }
130
+ }
131
+
132
+ # For 3x3 puzzle, solvable if inversions are even
133
+ return(inversions %% 2 == 0)
134
+ }
135
+
136
+ # Validate puzzle state
137
+ validate_puzzle <- function(puzzle) {
138
+ # Check length
139
+ if(length(puzzle) != 9) {
140
+ return(FALSE)
141
+ }
142
+
143
+ # Check if all numbers 0-8 are present exactly once
144
+ expected_tiles <- 0:8
145
+ if(!setequal(puzzle, expected_tiles)) {
146
+ return(FALSE)
147
+ }
148
+
149
+ # Check if solvable
150
+ if(!is_solvable(puzzle)) {
151
+ return(FALSE)
152
+ }
153
+
154
+ return(TRUE)
155
+ }
156
+
157
+ # Generate test puzzles for development
158
+ generate_test_puzzles <- function(n = 5) {
159
+ puzzles <- list()
160
+
161
+ for(i in 1:n) {
162
+ repeat {
163
+ puzzle <- create_solvable_puzzle()
164
+ if(validate_puzzle(puzzle) && !is_solved(puzzle)) {
165
+ puzzles[[i]] <- puzzle
166
+ break
167
+ }
168
+ }
169
+ }
170
+
171
+ return(puzzles)
172
+ }
173
+
174
+ # Simple puzzle statistics
175
+ get_puzzle_stats <- function(puzzle) {
176
+ blank_pos <- get_blank_position(puzzle)
177
+ valid_moves <- get_valid_moves(blank_pos)
178
+
179
+ return(list(
180
+ blank_position = blank_pos,
181
+ valid_moves = valid_moves,
182
+ num_valid_moves = length(valid_moves),
183
+ is_solved = is_solved(puzzle),
184
+ is_valid = validate_puzzle(puzzle)
185
+ ))
186
+ }