From: El O. De M. <obs...@gm...> - 2025-07-22 15:56:38
|
Hi I have used the command read many times and have noticed something like data persistence. You can try once the following attempted tic, tac, toe implementation, which uses read. --> /* Tic-Tac-Toe game implementation */ /* Initialize the game state */ /*grid: matrix(["", "", ""], ["", "", ""], ["", "", ""])$*/ grid:genmatrix(lambda([x,y],""),3,3)$ player: X$ --> /* Function to make a move */ make_move(my_row,my_col) := block(if grid[my_row][my_col] = "" then (grid[my_row][my_col]: player, player: if player = X then O else X) else (print("Invalid move, try again.")))$ --> diagonalp(grid):=is(unique(makelist(grid[i,i],i,3))#[""] and length(unique(makelist(grid[i,i],i,3)))=1)$ --> antidiagonalp(grid):=is(unique(makelist(grid[i,4-i],i,3))#[""] and length(unique(makelist(grid[i,4-i],i,3)))=1)$ --> rowp(grid):=block( L:[], for i from 1 thru 3 do if unique(flatten(args(row(grid,i))))#[""] and length(unique(flatten(args(row(grid,i)))))=1 then push(row(grid,i),L), if L#[] then true )$ --> columnp(grid):=block( L:[], for i from 1 thru 3 do if unique(flatten(args(col(grid,i))))#[""] and length(unique(flatten(args(col(grid,i)))))=1 then push(col(grid,i),L), if L#[] then true )$ --> check_winner(grid):=is(diagonalp(grid) or antidiagonalp(grid) or rowp(grid) or columnp(grid))$ --> /* Game loop */ tictactoe():=block( runner:true, while runner do ( print(grid), [my_row,my_col]: read("Enter row and col (1-3) as a list: "), make_move(my_row,my_col), winner: check_winner(grid), if winner then ( print("Player ", if player = X then O else X, " wins!"), runner:false), if not member("", flatten(args(grid))) then ( print("It's a draw!"), runner:false ) ) )$ --> reset_game() := ( grid: genmatrix(lambda([x,y],""),3,3), player: X )$ --> tictactoe(); After one play, if another game is initialized, a "suggestion" is shown for the player, namely the initial for the previous play. I mean, Is it good that while using read some "options" appear? There is another way to see the persistence behaviour. I could use vscode copilot in agent mode to ask it to do changes in .wxm files. One file in which I ask for changes was one with the code above, but after a play of tic, tac, toe additional lines were added, like: /* [wxMaxima: input start ] */ columnp(grid):=block( L:[], for i from 1 thru 3 do if unique(flatten(args(col(grid,i))))#[""] and length(unique(flatten(args(col(grid,i)))))=1 then push(col(grid,i),L), if L#[] then true )$ /* [wxMaxima: input end ] */ or /* [wxMaxima: question start ] */ <math><st>Enter column (1-3): </st><st> </st></math> /* [wxMaxima: question end ] */ /* [wxMaxima: answer start ] */ 2;3;3;2;1; /* [wxMaxima: answer end ] */ These lines persisted in vscode even if the wxmaxima code was returned to an unevaluated form (all cells not evaluated) and saved. If this kind of behavior is wanted, let me know best regards -- https://github.com/Observatorio-de-Matematica |