dimanche 1 octobre 2017

How do you make a simple 3x3 maze on python using lists?

I have this project wherein I have to make a basic text-based maze/adventure game. This is my code so far but it's weird. I want it to be able to quit anytime and for it to display 'Please choose an available direction' when the input is not among the choices. I know my code is wrong but I don't know what to do about it. Can someone help me out? Thanks!

btw, I used this link as a guide http://ift.tt/2xTcbHx

maze_list = []
maze = ['Available Directions: East', None, 1, None, None]
maze_list.append(maze)
maze = ['Available Directions: East, South', None, 2, 3, 0]
maze_list.append(maze)
maze = ['Available Directions: West', None, None, None, 1]
maze_list.append(maze)
maze = ['Available Directions: North, East', 1, 4, None, None]
maze_list.append(maze)
maze = ['Available Directions West, South', None, None, 5, 3]
maze_list.append(maze)


current_tile = 0
print(maze_list[0])


done = False
while done != True:

    print('')
    print(maze_list[current_tile][0]) # available directions
    move = raw_input('Which direction will you take?' )
    if move == 'East' or 'east' or 'EAST':
        next_tile = maze_list[current_tile][2]
        current_tile = next_tile

        print('')
        print(maze_list[current_tile][0])
        move = raw_input('Which direction will you take? ')
        if move == 'South' or 'south' or 'SOUTH':
          next_tile = maze_list[current_tile][3]
          current_tile = next_tile

          print('')
          print(maze_list[current_tile][0])
          move = raw_input('Which direction will you take? ')
          if move == 'East' or 'east' or 'EAST':
            next_tile = maze_list[current_tile][2]
            current_tile = next_tile

            print('')
            print(maze_list[current_tile][0])
            move = raw_input('Which direction will you take? ')
            if move == 'South' or 'south' or 'SOUTH':
              next_tile = maze_list[current_tile][3]
              current_tile = next_tile
              print('Nice')


              if move == 'Quit' or 'quit' or 'QUIT':
                print('Better luck next time. \nGoodbye.')
              elif move == 'West' or 'west' or 'WEST':
                next_tile = maze_list[4]
                current_tile = next_tile
              else:
                print('Please choose an available direction')

          elif move == 'Quit' or 'quit' or 'QUIT':
            print('Better luck next time. \nGoodbye.')
          elif move == 'North' or 'north' or 'NORTH':
            next_tile = maze_list[1]
            current_tile = next_tile
          else:
            print('Please choose an available direction')

        elif move == 'Quit' or 'quit' or 'QUIT':
          print('Better luck next time. \nGoodbye.')
        elif move == 'East' or 'east' or 'EAST':
          next_tile = maze_list[current_tile][2]
          current_tile = next_tile
        else:
          print('Please choose an available direction')

    elif move == 'Quit' or 'quit' or 'QUIT':
      print('Better luck next time. \nGoodbye.')
        else:
        print('Please choose an available direction.')

How to make the if statement more faster?

I have an if statement as follows:

coeff = zeros(1,255);
for ii = 1:255
    if (alpha1 == 0)
        error('Trying to retrieve the multiplicative inverse of 0!')
    elseif (alpha1 == 1)
        coeff(ii) = ii;        
    elseif (ii == 1)
        coeff(ii) = alpha1;
    else
        coeff(ii) = func( ii, alpha1 );
    end
end

where func is another function. How could I make my code faster because when I check time-consuming, the elseif state takes a lot of time? Thanks

JavaScript check for input only works backwards

When I fill in all the input fields and choose an option in the select dropdown, for some reason the class will not be removed unless I start with the select dropdown. How come is that? I really don't get it.

Here's my JavaScript:

function checkForInput() {
    var copyButton = document.getElementById('copyButton');
    var inputFields = firstNameInput.value && lastNameInput.value && emailInput.value && jobTitleInput.value && departmentSelect.options[departmentSelect.selectedIndex].value;

    if(inputFields != '') {
        copyButton.classList.remove('disabled');
    } else {
        copyButton.classList.add('disabled');
    }
}

And my HTML:

<div class="container">
  <div class="row">
    <form class="col s12">
      <div class="row">
        <div class="input-field col m6 s12">
          <input id="first_name_input" type="text" class="validate" onkeyup="firstNamePrev(); checkForInput();">
          <label for="first_name">Fornavn</label>
        </div>
        <div class="input-field col m6 s12">
          <input id="last_name_input" type="text" class="validate" onkeyup="lastNamePrev(); checkForInput();">
          <label for="last_name">Efternavn</label>
        </div>
      </div>
      <div class="row">
        <div class="input-field col m4 s12">
          <input id="email_input" type="email" class="validate" onkeyup="emailPrev(); checkForInput();">
          <label for="email">Email</label>
        </div>
        <div class="input-field col m4 s12">
          <input id="job_title_input" type="text" class="validate" onkeyup="jobTitlePrev(); checkForInput();">
          <label for="job_title">Jobtitel</label>
        </div>
                <div class="input-field col m4 s12">
                    <select id="department_select" onchange="departmentPrev();" onkeyup="checkForInput();">
                        <option value="" disabled selected>Afdeling</option>
                        <option value="administration">Administration</option>
                        <option value="marketing">Marketing</option>
                        <option value="support">Support</option>
                        <option value="reklamation">Reklamation</option>
                        <option value="produktion">Produktion</option>
                    </select>
                </div>
      </div>
    </form>
  </div>

Skipping if statement python tic-tac-toe [duplicate]

Can anyone explain to me why I still get an error?

board = [['#', '#', '#'],['#', '#', '#'],['#', '#', '#']]

def player_1(board):
a = int(raw_input('Choose position 1-9\n'))
if a == 1 or 2 or 3:
    if board[0][a-1] == '#':
        board[0][a-1] = 'x'
    else:
        print 'Move invalid, place is taken.'
        player_1(board)
if a == 4 or 5 or 6:
    if board[1][a-4] == '#':
        board[1][a-4] = 'x'
    else:
        print 'Move invalid, place is taken.'
        player_1(board)
if a == 7 or 8 or 9:
    if board[2][a-7] == '#':
        board[2][a-7] = 'x'
    else:
        print 'Move invalid, place is taken.'
        player_1(board)

When my raw_input = 4, I get this error message:

if board[0][a-1] == '#':
IndexError: list index out of range

Why does it not skip the first if statement?

NULL pointer as condition of if stament [duplicate]

I wonder why the output is not as I expected. Calling the function print at the first time, the if statement is none, so the out put should be aa, why IPtr isNULL, therefore, you see this instead?

   #include<stdio.h>

   void print(int *wm);

   int main()
   {
       int * IPtr;
       printf("%d\n", IPtr == NULL);
       print(IPtr);
      // After executing this statement, it's supposed to print <aa> on the screen.

      int a;
      IPtr = &a;

      print(IPtr);

      return 0;
  }

  void print(int *wm){
      if(wm){printf("IPtr is null, therefore, you see this\n");}
      else{ printf("aa\n");}
  }

I'm using int parse same way as it is in the console application but doesn't work in winforms

I need some help because I can't seem to get to display the text label here. I am using Windows Form C# VS 2015. When I press enter the error is on barangay = int.Parse(lblDistrict.Text); uhm also, I am applying the same way console.readline is used but it seems that it doesn't work. Can somebody help me in the code? :) Thanks in advance

private void txtBarangay_KeyPress(object sender, KeyPressEventArgs e)
{

    int barangay = 0;
    barangay = int.Parse(lblDistrict.Text);

    if (e.KeyChar == (char)13)
    {
        if (barangay >= 1 && barangay <= 146)
        {
            lblDistrict.Text = "District 1";
        }
        else if (barangay >= 147 && barangay <= 267)
        {
            lblDistrict.Text = "District 2";
        }
    }
}

Confused on how to evaluate the while !result in the while function

I'm not sure how to evaluate this line to understand what my code does while (i < data.length && !result) {

this is my full code.

public static boolean {2,6,-3,7,3} (int[] data, int val) {

boolean result = false;
int i = 0;

while (i < data.length && !result) {
    if (data[i] == val) {
        result = true;
    }
    i++;
}

return result;

}