rem KIM
rem
rem Author: Jacob Busby
rem
rem Change History
rem
rem xx/07/01  Initial development completed

TRUE = 1
FALSE = 0

rem PS2 Controller variables

UP = 16
DOWN = 64
LEFT = 128
RIGHT = 32
OH = 8192
EX = 16384
TRI = 4096
SQU = 32768
R1 = 2048
R2 = 512
L1 = 1024
L2 = 256

ROWS = 4
COLS = 13

X_OFFSET = 50
Y_OFFSET = 40
FLASHLENGTH = 0.05

Set_Up()
Randomise_Values()
open window 640, 512
rem while (TRUE = TRUE)
  Game_Loop()
rem wend

rem Game Loop starts here

sub Game_Loop()

  while (tiles_left > 0)
    DrawBuffer = 0
    Read_Controller()
    Draw_Tiles()
    Determine_Matches()
    setdispbuf DrawBuffer
    DrawBuffer = 1 - DrawBuffer
  wend

end sub


exit

rem -----

sub Determine_Matches()

  if selection(2,2) <> 0 then
    score = score - 1
    if values(selection(1,2),selection(1,1),1) = values(selection(2,2),selection(2,1),1) then
      bell
      values(selection(1,2),selection(1,1),2) = TRUE 
      values(selection(2,2),selection(2,1),2) = TRUE
      tiles_left = tiles_left - 2
    fi

rem wait for keypress

    label wait_for_o
    KeyPress = peek("port1")
    if (KeyPress <> OH) then
      wait 0.05
      goto wait_for_o
    fi

    values(selection(1,2),selection(1,1),3) = FALSE 
    values(selection(2,2),selection(2,1),3) = FALSE
    selection(1,1) = 0
    selection(1,2) = 0
    selection(2,1) = 0
    selection(2,2) = 0
  fi 

end sub

rem ------------------------------------------------------
rem ------------------------------------------------------

sub Draw_Tiles()

  for index = 1 to ROWS
    for jndex = 1 to COLS
      if values(index,jndex,2) = FALSE and values(index,jndex,3) = FALSE then
        setrgb 1, 64, 0, 0
        if jndex = cursor_x and index = cursor_y then
           wait FLASHLENGTH
           if flash = TRUE setrgb 1,0,0,0
        fi
        fill rectangle X_OFFSET + ((40 * jndex) - 20), Y_OFFSET + (40 * index) - 20 to X_OFFSET + (40 * jndex), Y_OFFSET + (40 * index)
      else

rem Give the tile a black background

        setrgb 1,0,0,0
        fill rectangle X_OFFSET + ((40 * jndex) - 20), Y_OFFSET + (40 * index) - 20 to X_OFFSET + (40 * jndex), Y_OFFSET + (40 * index)

rem Detremine "colour" of text.

        setrgb 1, 255, 255, 255
        if jndex = cursor_x and index = cursor_y then
          wait FLASHLENGTH
          if flash = TRUE setrgb 1,0,0,0
        fi
        text X_OFFSET + ((40 * jndex) - 15), Y_OFFSET + ((40 * index) - 5), chr$((values(index,jndex,1) + 64))
      fi
    next jndex
  next index
  flash = TRUE - flash

end sub

rem ------------------------------------------------------
rem ------------------------------------------------------

sub Read_Controller()

  KeyPressed = TRUE
  KeyPress = peek("port1")
  if (KeyPress = UP) then
    cursor_y = cursor_y - 1
    if cursor_y = 0 then
      cursor_y = ROWS
    fi
  elsif (KeyPress = DOWN) then
    cursor_y = cursor_y + 1
    if cursor_y > ROWS then
      cursor_y = 1
    fi
  elsif (KeyPress = LEFT) then
    cursor_x = cursor_x - 1
    if cursor_x = 0 then
      cursor_x = COLS
    fi
  elsif (KeyPress = RIGHT) then
    cursor_x = cursor_x + 1
    if cursor_x > COLS then
      cursor_x = 1
    fi
  elsif (KeyPress = L2) then
    cursor_x = 1
    cursor_y = 1
  elsif (KeyPress = L1) then
    cursor_x = 1
    cursor_y = ROWS
  elsif (KeyPress = R2) then
    cursor_x = COLS
    cursor_y = 1
  elsif (KeyPress = R1) then
    cursor_x = COLS
    cursor_y = ROWS
  elsif (KeyPress = EX) then
    values(cursor_y,cursor_x,3) = TRUE
    if selection(1,1) = 0 then
      selection(1,1) = cursor_x
      selection(1,2) = cursor_y
    else
      if cursor_x <> selection(1,1) or cursor_y <> selection(1,2) then
        selection(2,1) = cursor_x
        selection(2,2) = cursor_y
      fi
    fi
  else KeyPressed = FALSE
  fi

  rem Housekeeping: 
  rem Wait a moment to prevent over-responsive controls.

  if KeyPressed = TRUE flash = FALSE
  wait 0.2

end sub

rem ===== ADMINISTRATION ROUTINES =====

rem ------------------------------------------------------
rem ------------------------------------------------------

sub Set_Up()

  rem values: x, y, data control
  rem 
  rem x is the x position of the object
  rem y is the y position of the object
  rem Data Control is made up of the following:
  rem  element 1: The value which is being hidden
  rem  element 2: A Boolean flag to detremine if
  rem             the variable is matched or not
  rem  element 3: A boolean flag to determine if
  rem             the variable should be shown or not

  dim values(ROWS,COLS,3)

  rem Now initialise the array.

  for index = 1 to ROWS
    for jndex = 1 to COLS
      values(ROWS,COLS,1) = 0
      values(ROWS,COLS,2) = FALSE
      values(ROWS,COLS,3) = FALSE
    next jndex
  next index    

  dim selection(2,2)

rem selection: selection number, coordinates
rem
rem This array indicates the selection that the user has 
rem made this turn.
rem
rem selection: Either 1 (first guess) or 2 (second guess)
rem coordinates: The co-ordinates of the guess, such that
rem  1: x-co-ordinate
rem  2: y-co-ordinate

  for index = 1 to 2
    for jndex = 1 to 2
      selection(index,jndex) = 0
    next jndex
  next index


  cursor_x = 1
  cursor_y = 1
  tiles_left = ROWS * COLS

end sub

rem ------------------------------------------------------
rem Randomise_Values
rem
rem This routine creates the values of the Values array.
rem ----------------------------------------------------- 

sub Randomise_Values()

  for n = 1 to ((ROWS * COLS) / 2)
    label PickFirstAgain
    a = int(ran(ROWS * COLS)) + 1
    a_cols = mod(a,COLS)
    if a_cols = 0 a_cols = COLS
    a_rows = (a + COLS - a_cols) / COLS
    if values(a_rows,a_cols,1) = 0 then
      values(a_rows, a_cols, 1) = n
    else
      goto PickFirstAgain
    fi
    label PickSecondAgain
    b = int(ran(ROWS * COLS)) + 1
    b_cols = mod(b,COLS)
    if b_cols = 0 b_cols = COLS
    b_rows = (b + COLS - b_cols) / COLS
    if values(b_rows, b_cols, 1) = 0 then
      values(b_rows, b_cols, 1) = n
    else
      goto PickSecondAgain
    fi
  next n

rem De-rem the following lines for debugging purposes

rem  for i = 1 to ROWS
rem    for j = 1 to COLS
rem      print chr$(values(i,j,1) + 64);
rem    next j
rem    print
rem  next i

end sub



