Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Avoid input repetition
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
TheSoldierDude




PostPosted: Sat Jun 01, 2013 2:34 pm   Post subject: Avoid input repetition

What is it you are trying to achieve?
I'm trying to stop the user from inputting the same information over and over again. In this case the user will be entering all the information the same time.


What is the problem you are having?
I can't seem to get my head around this.


Describe what you have tried to solve this problem
I've used a series of loops and if statements to put the inputs from the user into an array and then comparing the past inputs with the recent ones. but so far I can't get it to work.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
N/A



Please specify what version of Turing you are using
4.1
 
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sat Jun 01, 2013 2:39 pm   Post subject: RE:Avoid input repetition

Why is your code n/a?
 
Insectoid




PostPosted: Sat Jun 01, 2013 2:46 pm   Post subject: RE:Avoid input repetition

It sounds like you're already halfway there. You need to keep track of how many inputs have been received. Every time you add an input, loop over the array from 1 to the number of inputs, comparing the new input to all the old ones. If the comparison ever returns true (the two inputs are equal), then the input is a duplicate and should be discarded.
 
TheSoldierDude




PostPosted: Sat Jun 01, 2013 3:21 pm   Post subject: Re: RE:Avoid input repetition

Raknarg @ Sat Jun 01, 2013 2:39 pm wrote:
Why is your code n/a?


Turing:


import GUI
setscreen ("graphics:1280;720")
var startPicture : int := Pic.FileNew ("Battleship.bmp")
Pic.Draw (startPicture, 1, 1, 0)
delay (2000)
cls
Pic.Free (startPicture)
var playerGrid : array 'A' .. 'F', 1 .. 6 of string
var compGrid : array 'A' .. 'F', 1 .. 6 of int
var fakeGrid : array 'A' .. 'F', 1 .. 6 of string
for row : 'A' .. 'F'
    for col : 1 .. 6
        playerGrid (row, col) := "_"
        compGrid (row, col) := 1
    end for
end for
var exitHelper : int := 0
var exitHelper2 : int := 0
var exitHelperUser : int := 0
var exitHelperUser2 : int := 0
var rowAndColFont : int := Font.New ("Comic Sans MS:40")
var sentences : int := Font.New ("Comic Sans MS:24:bold,italic")
const horizontalBoxes : int := 7
const verticleBoxes : int := 7
const SpaceSize : int := 2

function verticleLine (num : int) : int
    result ((maxx div verticleBoxes) * num)
end verticleLine

function horizontalLine (num : int) : int
    result ((maxy div horizontalBoxes) * num)
end horizontalLine


procedure userGrid
    for col : 1 .. verticleBoxes
        drawline (verticleLine (col), 0, verticleLine (col), maxy, 7)
    end for
    for row : 1 .. horizontalBoxes
        drawline (0, horizontalLine (row), maxx, horizontalLine (row), 7)
    end for

    Font.Draw ("A", maxx div 12, 550, rowAndColFont, black)
    Font.Draw ("B", maxx div 12, 450, rowAndColFont, black)
    Font.Draw ("C", maxx div 12, 350, rowAndColFont, black)
    Font.Draw ("D", maxx div 12, 250, rowAndColFont, black)
    Font.Draw ("E", maxx div 12, 150, rowAndColFont, black)
    Font.Draw ("F", maxx div 12, 50, rowAndColFont, black)

    Font.Draw ("1", 265, 650, rowAndColFont, black)
    Font.Draw ("2", 450, 650, rowAndColFont, black)
    Font.Draw ("3", 625, 650, rowAndColFont, black)
    Font.Draw ("4", 800, 650, rowAndColFont, black)
    Font.Draw ("5", 985, 650, rowAndColFont, black)
    Font.Draw ("6", 1175, 650, rowAndColFont, black)

    Font.Draw (playerGrid ('A', 1), 265, 550, rowAndColFont, green)
    Font.Draw (playerGrid ('A', 2), 450, 550, rowAndColFont, green)
    Font.Draw (playerGrid ('A', 3), 625, 550, rowAndColFont, green)
    Font.Draw (playerGrid ('A', 4), 800, 550, rowAndColFont, green)
    Font.Draw (playerGrid ('A', 5), 985, 550, rowAndColFont, green)
    Font.Draw (playerGrid ('A', 6), 1175, 550, rowAndColFont, green)

    Font.Draw (playerGrid ('B', 1), 265, 450, rowAndColFont, green)
    Font.Draw (playerGrid ('B', 2), 450, 450, rowAndColFont, green)
    Font.Draw (playerGrid ('B', 3), 625, 450, rowAndColFont, green)
    Font.Draw (playerGrid ('B', 4), 800, 450, rowAndColFont, green)
    Font.Draw (playerGrid ('B', 5), 985, 450, rowAndColFont, green)
    Font.Draw (playerGrid ('B', 6), 1175, 450, rowAndColFont, green)

    Font.Draw (playerGrid ('C', 1), 265, 350, rowAndColFont, green)
    Font.Draw (playerGrid ('C', 2), 450, 350, rowAndColFont, green)
    Font.Draw (playerGrid ('C', 3), 625, 350, rowAndColFont, green)
    Font.Draw (playerGrid ('C', 4), 800, 350, rowAndColFont, green)
    Font.Draw (playerGrid ('C', 5), 985, 350, rowAndColFont, green)
    Font.Draw (playerGrid ('C', 6), 1175, 350, rowAndColFont, green)

    Font.Draw (playerGrid ('D', 1), 265, 250, rowAndColFont, green)
    Font.Draw (playerGrid ('D', 2), 450, 250, rowAndColFont, green)
    Font.Draw (playerGrid ('D', 3), 625, 250, rowAndColFont, green)
    Font.Draw (playerGrid ('D', 4), 800, 250, rowAndColFont, green)
    Font.Draw (playerGrid ('D', 5), 985, 250, rowAndColFont, green)
    Font.Draw (playerGrid ('D', 6), 1175, 250, rowAndColFont, green)

    Font.Draw (playerGrid ('E', 1), 265, 150, rowAndColFont, green)
    Font.Draw (playerGrid ('E', 2), 450, 150, rowAndColFont, green)
    Font.Draw (playerGrid ('E', 3), 625, 150, rowAndColFont, green)
    Font.Draw (playerGrid ('E', 4), 800, 150, rowAndColFont, green)
    Font.Draw (playerGrid ('E', 5), 985, 150, rowAndColFont, green)
    Font.Draw (playerGrid ('E', 6), 1175, 150, rowAndColFont, green)

    Font.Draw (playerGrid ('F', 1), 265, 50, rowAndColFont, green)
    Font.Draw (playerGrid ('F', 2), 450, 50, rowAndColFont, green)
    Font.Draw (playerGrid ('F', 3), 625, 50, rowAndColFont, green)
    Font.Draw (playerGrid ('F', 4), 800, 50, rowAndColFont, green)
    Font.Draw (playerGrid ('F', 5), 985, 50, rowAndColFont, green)
    Font.Draw (playerGrid ('F', 6), 1175, 50, rowAndColFont, green)


end userGrid
userGrid

procedure oppGrid
    colour (red)
    put "The Computer's Grid"
    put "    1  2  3  4  5  6"
    put "A   ", compGrid ('A', 1), "  ", compGrid ('A', 2), "  ", compGrid ('A', 3), "  ", compGrid ('A', 4), "  ", compGrid ('A', 5), "  ", compGrid ('A', 6)
    put "B   ", compGrid ('B', 1), "  ", compGrid ('B', 2), "  ", compGrid ('B', 3), "  ", compGrid ('B', 4), "  ", compGrid ('B', 5), "  ", compGrid ('B', 6)
    put "C   ", compGrid ('C', 1), "  ", compGrid ('C', 2), "  ", compGrid ('C', 3), "  ", compGrid ('C', 4), "  ", compGrid ('C', 5), "  ", compGrid ('C', 6)
    put "D   ", compGrid ('D', 1), "  ", compGrid ('D', 2), "  ", compGrid ('D', 3), "  ", compGrid ('D', 4), "  ", compGrid ('D', 5), "  ", compGrid ('D', 6)
    put "E   ", compGrid ('E', 1), "  ", compGrid ('E', 2), "  ", compGrid ('E', 3), "  ", compGrid ('E', 4), "  ", compGrid ('E', 5), "  ", compGrid ('E', 6)
    put "F   ", compGrid ('F', 1), "  ", compGrid ('F', 2), "  ", compGrid ('F', 3), "  ", compGrid ('F', 4), "  ", compGrid ('F', 5), "  ", compGrid ('F', 6)
end oppGrid

procedure computerGrid

    for col : 1 .. verticleBoxes
        drawline (verticleLine (col), 0, verticleLine (col), maxy, 7)
    end for
    for row : 1 .. horizontalBoxes
        drawline (0, horizontalLine (row), maxx, horizontalLine (row), 7)
    end for

    Font.Draw ("A", maxx div 12, 550, rowAndColFont, black)
    Font.Draw ("B", maxx div 12, 450, rowAndColFont, black)
    Font.Draw ("C", maxx div 12, 350, rowAndColFont, black)
    Font.Draw ("D", maxx div 12, 250, rowAndColFont, black)
    Font.Draw ("E", maxx div 12, 150, rowAndColFont, black)
    Font.Draw ("F", maxx div 12, 50, rowAndColFont, black)

    Font.Draw ("1", 265, 650, rowAndColFont, black)
    Font.Draw ("2", 450, 650, rowAndColFont, black)
    Font.Draw ("3", 625, 650, rowAndColFont, black)
    Font.Draw ("4", 800, 650, rowAndColFont, black)
    Font.Draw ("5", 985, 650, rowAndColFont, black)
    Font.Draw ("6", 1175, 650, rowAndColFont, black)

    Font.Draw (fakeGrid ('A', 1), 265, 550, rowAndColFont, red)
    Font.Draw (fakeGrid ('A', 2), 450, 550, rowAndColFont, red)
    Font.Draw (fakeGrid ('A', 3), 625, 550, rowAndColFont, red)
    Font.Draw (fakeGrid ('A', 4), 800, 550, rowAndColFont, red)
    Font.Draw (fakeGrid ('A', 5), 985, 550, rowAndColFont, red)
    Font.Draw (fakeGrid ('A', 6), 1175, 550, rowAndColFont, red)

    Font.Draw (fakeGrid ('B', 1), 265, 450, rowAndColFont, red)
    Font.Draw (fakeGrid ('B', 2), 450, 450, rowAndColFont, red)
    Font.Draw (fakeGrid ('B', 3), 625, 450, rowAndColFont, red)
    Font.Draw (fakeGrid ('B', 4), 800, 450, rowAndColFont, red)
    Font.Draw (fakeGrid ('B', 5), 985, 450, rowAndColFont, red)
    Font.Draw (fakeGrid ('B', 6), 1175, 450, rowAndColFont, red)

    Font.Draw (fakeGrid ('C', 1), 265, 350, rowAndColFont, red)
    Font.Draw (fakeGrid ('C', 2), 450, 350, rowAndColFont, red)
    Font.Draw (fakeGrid ('C', 3), 625, 350, rowAndColFont, red)
    Font.Draw (fakeGrid ('C', 4), 800, 350, rowAndColFont, red)
    Font.Draw (fakeGrid ('C', 5), 985, 350, rowAndColFont, red)
    Font.Draw (fakeGrid ('C', 6), 1175, 350, rowAndColFont, red)

    Font.Draw (fakeGrid ('D', 1), 265, 250, rowAndColFont, red)
    Font.Draw (fakeGrid ('D', 2), 450, 250, rowAndColFont, red)
    Font.Draw (fakeGrid ('D', 3), 625, 250, rowAndColFont, red)
    Font.Draw (fakeGrid ('D', 4), 800, 250, rowAndColFont, red)
    Font.Draw (fakeGrid ('D', 5), 985, 250, rowAndColFont, red)
    Font.Draw (fakeGrid ('D', 6), 1175, 250, rowAndColFont, red)

    Font.Draw (fakeGrid ('E', 1), 265, 150, rowAndColFont, red)
    Font.Draw (fakeGrid ('E', 2), 450, 150, rowAndColFont, red)
    Font.Draw (fakeGrid ('E', 3), 625, 150, rowAndColFont, red)
    Font.Draw (fakeGrid ('E', 4), 800, 150, rowAndColFont, red)
    Font.Draw (fakeGrid ('E', 5), 985, 150, rowAndColFont, red)
    Font.Draw (fakeGrid ('E', 6), 1175, 150, rowAndColFont, red)

    Font.Draw (fakeGrid ('F', 1), 265, 50, rowAndColFont, red)
    Font.Draw (fakeGrid ('F', 2), 450, 50, rowAndColFont, red)
    Font.Draw (fakeGrid ('F', 3), 625, 50, rowAndColFont, red)
    Font.Draw (fakeGrid ('F', 4), 800, 50, rowAndColFont, red)
    Font.Draw (fakeGrid ('F', 5), 985, 50, rowAndColFont, red)
    Font.Draw (fakeGrid ('F', 6), 1175, 50, rowAndColFont, red)


end computerGrid
for row : 'A' .. 'F'
    for col : 1 .. 6
        fakeGrid (row, col) := "_"
    end for
end for

loop

    var choice : string
    var first : char
    var second : int
    var changer : string
    var adder : int := 0
    var adder2 : int := 0
    var random : int
    var checker : int := 0
    var goBack : string (1)

    loop
        exit when adder = 8
        userGrid
        put ""
        put "Please Enter where you wish to place your ships, (Example: A5)..." ..
        get choice
        cls
        if choice = "cheat" or choice = "Cheat" or choice = "CHEAT" then
            put "You cheated to look at your opponent's ships"
            put "The computer hasn't chosen any ships to place yet"
            oppGrid
            put ""
            put "Press any key to go back"
            getch (goBack)
            cls

        end if
        if length (choice) = 2 then

            if choice (1) > 'F' or choice (1) < 'A' then
                put "Try again!"

            elsif choice (2) < "1" or choice (2) > "6" then
                put "Try again!"
            else
                adder := adder + 1
                for a : 1 .. 1
                    first := choice (a)
                end for
                for b : 2 .. 2
                    changer := choice (b)
                    second := strint (changer)
                end for
                playerGrid (first, second) := "S"
            end if
        else
            put "Try again!"
        end if

    end loop

    cls
    userGrid
    put ""
    put "The Computer is choosing it's Fleet, please stand by" ..
    delay (900)
    put "." ..
    delay (900)
    put "." ..
    delay (900)
    put "."

    for row : 'A' .. 'F'
        for col : 1 .. 6
            adder2 := adder2 + 1
            compGrid (row, col) := adder2
        end for
    end for

    loop
        exit when checker = 8
        randint (random, 1, 36)
        checker := checker + 1
        for row : 'A' .. 'F'
            for col : 1 .. 6
                if compGrid (row, col) = random then
                    compGrid (row, col) := 0
                end if
            end for
        end for

    end loop
    for row : 'A' .. 'F'
        for col : 1 .. 6
            if compGrid (row, col) not= 0 then
                compGrid (row, col) := 1
            end if
        end for
    end for
    for row : 'A' .. 'F'
        for col : 1 .. 6
            if compGrid (row, col) = 0 then
                exitHelper := exitHelper + 1
            end if
            if playerGrid (row, col) = "S" then
                exitHelperUser := exitHelperUser + 1
            end if
        end for
    end for

    loop
        var input : string
        var first2 : char
        var second2 : int
        var mover : string
        var compHits : int
        var compShipCounter : int := 0
        var userShipCounter : int := 0
        var compHitsFind : array 1 .. 36 of string := init ("A1", "A2", "A3", "A4", "A5", "A6", "B1", "B2", "B3", "B4", "B5", "B6", "C1", "C2", "C3", "C4", "C5", "C6", "D1", "D2", "D3", "D4",
            "D5",
            "D6", "E1", "E2", "E3", "E4", "E5", "E6", "F1", "F2", "F3", "F4", "F5", "F5")
        var compHitsStore : string
        var first3 : char
        var second3 : int
        var changer2 : string

        loop
            exit when exitHelper2 = exitHelper
            exit when exitHelperUser = exitHelperUser2
            cls
            computerGrid
            put "Pick a position on the opponent's grid to attack (Example: A1).." ..
            get input
            cls
            if input = "cheat" or input = "Cheat" or input = "CHEAT" then
                put "You cheated to look at your opponent's ships."
                put "The 0's are the spots where ships have been placed and the 9's the are spots ships have been destroyed at."
                put "1 is rest of the battlefield."
                oppGrid
                put ""
                put "Press any key to go back"
                getch (goBack)
                cls
                put "Press any key to start playing again!"
                getch (goBack)
                cls
            end if

            if length (input) = 2 then

                if input (1) > 'F' or input (1) < 'A' then
                    cls
                    computerGrid
                    put "Try again!"

                elsif input (2) < "1" or input (2) > "6" then
                    cls
                    computerGrid
                    put "Try again!"
                else
                    for b : 1 .. 1
                        first2 := input (b)
                    end for
                    for v : 2 .. 2
                        mover := input (v)
                        second2 := strint (mover)
                    end for
                    if compGrid (first2, second2) = 0 then
                        cls
                        put "You sunk a ship!"
                        exitHelper2 := exitHelper2 + 1
                        compGrid (first2, second2) := 9
                        fakeGrid (first2, second2) := "D"
                        computerGrid
                        delay (3000)
                    else
                        cls
                        put "You missed!"
                        compGrid (first2, second2) := 0
                        fakeGrid (first2, second2) := "M"
                        computerGrid
                        delay (3000)
                    end if
                    cls
                    put "Your opponent's turn!" ..
                    delay (900)
                    put "." ..
                    delay (900)
                    put "." ..
                    delay (900)
                    put "."
                    randint (compHits, 1, 36)
                    compHitsStore := compHitsFind (compHits)

                    for q : 1 .. 1
                        first3 := compHitsStore (q)
                    end for
                    for w : 2 .. 2
                        changer2 := compHitsStore (w)
                        second3 := strint (changer2)
                    end for
                    userGrid

                    if playerGrid (first3, second3) = "S" then
                        playerGrid (first3, second3) := "D"
                        cls
                        put "Your opponent sunk one of your ships!"
                        exitHelperUser2 := exitHelperUser2 + 1
                        userGrid
                        put "Press any key to take your turn!"
                        getch (goBack)
                    elsif playerGrid (first3, second3) = "_" then
                        playerGrid (first3, second3) := "M"
                        cls
                        put "Your opponent missed!"
                        userGrid
                        put "Press any key to take your turn!"
                        getch (goBack)
                    end if
                end if
            end if
        end loop

        if exitHelper = exitHelper2 then
            put "You have defeated your enemy! YOU WON!"
            exit
        elsif exitHelperUser = exitHelperUser2 then
            put "Your opponent has defeated you! All your ships are gone.. GAME OVER!"
            delay (3000)
            put "Your opponent's grid"
            oppGrid
            exit

        end if

    end loop
end loop

 
ncvitak




PostPosted: Sun Jun 09, 2013 7:10 pm   Post subject: Re: Avoid input repetition

What you could do is have a flexible array of string, and do something like this.

- Get Input From User
- Search the flexible array, to check if the value exists within the array

if (the value is in the array) {
- tell the user that the value has already been entered.
} else {
- append the new value onto the flexible array
- do whatever with the information
}

Haven't programmed turing in a while, but you should understand this so long as you have a knowledge of how to use flexible arrays in Turing Confused
 
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: