0

I'm sorry if the title is a little bit vague; I am not sure how to put my concern into words. I am a beginner in Python and I am making a Boggle/Word Search game.

Please view this image.

I would like the board to stay in the same place/not get pushed back by the user inputs. Is that possible?

I tried opening a .txt file in Python via Notepad but the code stops once the txt file is open; any help with this will do.

I wonder if it is also possible to make python open a new window/console to display the board while the other console goes on and executes the original code.

Finally, is there a way kind of 'refresh' the user inputs here?

I would like the line to be empty once the user submits their answer. I've tried making a GUI with tkinter but it was too difficult for me due to the number of pages that I made and that I really have no idea how to use it.

Thank you!

fewlinesofcode
  • 3,007
  • 1
  • 13
  • 30
  • Sorry but this is too broad currently. Without a reproducible example, it's hard to understand what your issue is. – roganjosh Nov 02 '18 at 09:23
  • You need to design and implement a human interface for your program. If you want to keep it text-based, depending on the OS, there may be libraries available the will allow positioning the cursor anywhere within the console (aka terminal) window and output text starting there.Search for things related to [curses](https://en.wikipedia.org/wiki/Curses_(programming_library)), one common way to do this. Your other choice is to have a graphical human interface. Python comes with a library named `tkinter` that can be used and there are other third-party ones, like `wxPython` available, too. – martineau Nov 02 '18 at 09:51
  • A fairly common practice way to do this in the idle is to create a buffer to make it appear that the board position never changes. This can be done with a series of newlines encapsulating your board. The exact number needed will depend on your need but here is an example. `print("\n"*100+my_board_text)` This print's 100 new lines followed by your board. – TheLazyScripter Nov 02 '18 at 10:04
  • 1
    For Windows, there's a `curses` build mentioned in [this answer](https://stackoverflow.com/a/19851287/355230) to the question [Curses alternative for windows](https://stackoverflow.com/questions/14779486/curses-alternative-for-windows). – martineau Nov 02 '18 at 11:09

2 Answers2

1

One approach you may use with python built-in means is to refresh all the data on the screen (clear and print again) when it is necessary. Try the sample code (should work in linux terminal and windows console):

import os
import sys

if sys.platform.startswith("win"):
    def clear_screen():
        os.system('cls')
else:
    def clear_screen():
        os.system('clear')

def print_board(board):
    for row in board:
        print(*row)

data = [[1, 1], [1, 1]]

clear_screen()
print_board(data)

diff = input('\nPlease enter difficulty (easy, medium, hard): ')

clear_screen()
print_board(data)

limit = input('\nEnter time limit in minutes: ')

words = list()
counter = 0
while True:
    clear_screen()
    print_board(data)
    print('\nTime limit is {} minutes.'.format(limit))
    print('\nInput the words you see.')
    words.append(input('>>> '))
    counter += 1
    if counter == 3:
        break

clear_screen()
print_board(data)
print('\nYou have entered the words:')
print(*words, sep=', ')

print('\nEND')
1

You have three possible solutions:

  1. reprint the whole screen as you want it displayed after user input
  2. create a GUI, where you have a lot more control over what is displayed
  3. if on Unix you can try using curses

Since, you've already tried tkinter, then the GUI route seems a no-go route for you currently. So you may have similar problems with curses as well. As such, I'd recommend sticking to just reprinting your screen as you want it. You'll end up with a history of previous boards and guesses, but it's an easy way to control what the screen looks like.

eg.

def mainloop(board, words, time_limit):
    correct_guesses = []
    print_board(board)
    while not solved(words, correct_guesses) and now() < time_limit:
         word = get_user_guess()
         if word in words and word not in correct_guesses:
             correct_guesses.append(word)
             print('correct!\n')
         else:
             print('wrong!\n')

    if solved(words, correct_guesses):
        print('complete!')
    else:
        print('times up!')

You'll end up with something like:

E B C D
C F G H
I J K L
M N O P
your guess: mouse
wrong!

E B C D
C F G H
I J K L
M N O P
your guess: mice
correct!

complete!

If you want to try curses here is a basic script to get you started:

import curses


def main(stdscr):
    board = [
        ['E', 'B', 'C', 'D'],
        ['C', 'F', 'G', 'H'],
        ['I', 'J', 'K', 'L'],
        ['M', 'N', 'O', 'P'],
    ]

    curses.echo()  # we want to see user input on screen

    user_guess_prompt = 'your guess: '
    guess_line = len(board) + 1 + 2
    guess_column = len(user_guess_prompt)
    max_guess_size = 15

    guess = None
    while guess != 'q':
        # clear the entire screen
        stdscr.clear()

        # a bit of help on how to quit (unlike print, we have to add our own new lines)
        stdscr.addstr('enter q as a guess to exit\n\n')

        # print the board
        for line in board:
            stdscr.addstr(' '.join(line)+'\n')

        # tell the use about their previous guess
        if guess == 'mice':
            stdscr.addstr(guess + ' is correct!\n')
        elif guess is None:
            stdscr.addstr('\n')
        else:
            stdscr.addstr(guess + ' is wrong!\n')

        # prompt for the user's guess
        stdscr.addstr(user_guess_prompt)

        # tell curses to redraw the screen
        stdscr.refresh()

        # get user input with the cursor initially placed at the given line and column 
        guess = stdscr.getstr(guess_line, guess_column, max_guess_size).decode('ascii')


if __name__ == '__main__':
    curses.wrapper(main)
Dunes
  • 37,291
  • 7
  • 81
  • 97