• Breaking News

    Monday, February 17, 2020

    How to get into GUI programming? Ask Programming

    How to get into GUI programming? Ask Programming


    How to get into GUI programming?

    Posted: 17 Feb 2020 02:33 PM PST

    I had my first semester in computer science. Programming classes were about CLI programming (Competitive coding, basic data structures, some algorithms, etc). I tried to get into GUI programming, but honestly, i have no idea where to start, because everything seems weird.

    I think being able to create simple programs that detect mouse movement and basic interations, like clicks by the user, or keyboard detections and displays on screen is a simple, yet efective way to start.

    I use python3, by the way

    Any tips on where to start?

    Thanks in advance

    submitted by /u/lcv2000
    [link] [comments]

    [REST API] What should I return to the client if there is a 500 error?

    Posted: 17 Feb 2020 03:55 PM PST

    I've been building the error handling part of the API and I'm not sure what the response should be when there are 500 code errors.

    I know that there are security implications by responding with the error name and message, since there is danger of exposing implementation details that might provide hackers important information.

    My plan so far is just to respond with a 500 message saying "Something went wrong. Please contact support", and then save an error log in the server.

    However, I think this is not useful at all for the API consumers. So I'm curious about what would be a better way to handle this kind of errors.

    submitted by /u/parceriwafer
    [link] [comments]

    Can you open a URL based on user input?

    Posted: 17 Feb 2020 09:31 PM PST

    Hi,

    I'd like to be able to open a webpage based on a URL+"user input".

    The first part of the URL is known, for example "https://docfind.myworkplace.com/something/?dN="

    the user input is a document number after the "=".

    I currently have the URL bookmarked, open it (Chrome) to a "failed to load page" then add the document number to the end.

    I'd like to hit a link or run a script to ask for 1 or multiple numbers - it would save a lot of time when searching for docs.

    Is this possible use Chrome or IE?

    submitted by /u/kilapeno
    [link] [comments]

    What are the Algorithms problems that haven't been solved yet?

    Posted: 17 Feb 2020 08:45 PM PST

    I'm trying to compile and run a working C++ program on Ubuntu and I have no idea what's happening.

    Posted: 17 Feb 2020 07:18 PM PST

    I'm working on an assignment for a class. I have the file working on my windows machine in CLion no problem. For this class however, I need to have it compile/run on a Linux box which I'm connecting to over PuTTY.

     

    Here's what happens when I try and compile/run it.

     

    Here are the files:

    books.cpp

    executive.cpp

    books.h

     

    Any ideas? I'm considering a priest.

    submitted by /u/ethanhen
    [link] [comments]

    Simple script for website logging

    Posted: 17 Feb 2020 07:01 PM PST

    Hi,

    I used to do programming years ago (C, Perl, SQL), and can write simple stuff with google search. However, I've never done web programming and would like some guideline or better yet a script template itself.

    So I have a website for which I need a script that will login and go to a page. I downloaded AutoHotKey and got a script (below) -

    web_browser := ComObjCreate("InternetExplorer.Application")

    web_browser.Visible := true

    web_browser.Navigate("")

    while web_browser.busy

    sleep 100 

    sleep 1000

    username_input := web_browser.document.getElementById("email")

    username_input.value := My_User_Name

    password_input := web_browser.document.getElementById("pass")

    password_input.value := My_Pass_Word

    web_browser.document.getElementById("u_0_n").click()

    return

    But the script does not work because my website login is not on the page itself. There is a "My Account" link which opens a popup widget where I've to enter my credentials.

    My ultimate goal is to open 3 instances of this website (3 different credentials) in Chrome by using "Multi Session Box" extension (or any other trick that lets me open same website with different sessions side-by-side). But lets keep that aside for a while, even if I'm able to automate 1 session, thats okay too.

    submitted by /u/Mr_Bean12
    [link] [comments]

    AI/ML

    Posted: 17 Feb 2020 12:32 PM PST

    Hi everyone, i am a cs student currently in my second year, but i am trying to find ideas for my final project (capstone), i am very fascinated by artificial intelligence. I am thinking of building a smart conversational interface ( something like Alexa and siri) but really i dont know where to start. I am used to work with C and java. Is there any projects i can start working on and what are the languages and the tools i need to learn and use. Thanks

    submitted by /u/xadimi
    [link] [comments]

    List of Graph Theory Algorithms?

    Posted: 17 Feb 2020 05:23 PM PST

    Algorithms tend to have names that I would never guess. This makes it hard for me to find the right algorithm that I need. It might be easier if I was given a list of algorithms and I manually look through them to pick one which best solves my problem.

    If anyone could identify an algorithm that could help, that would be great too!

    My problem is related to graph theory. I have a list of university classes. Classes have prerequisites, for example before you can take this class you need to take X other classes from {subarray of classes}. The prerequisites form a directed cyclic graph. For a given class, what is the minimum number of other classes that need to be taken such that the prerequisites of all taken classes are met?

    submitted by /u/jakbrtz
    [link] [comments]

    Explain functioning in web architecture?

    Posted: 17 Feb 2020 04:31 PM PST

    (React Redux) Is this a possible spelling error on Tutorial Point?

    Posted: 17 Feb 2020 04:01 PM PST

    Hello, I am using Tutorial Point to understand about Redux (Link: https://www.tutorialspoint.com/redux/redux_react_example.htm )

    In the sample code '/src/app.js' provided in the link above, following line i provided below seems inaccurate to me or maybe i am not just understanding the flow.

    import Counter from '../src/container/appContainer'; 

    Instead of importing from 'appContainer', shouldn't it be 'counterContainer' which is provided also provided in the link above? Otherwise, i am not sure how exactly it's importing 'Counter' in app.js

    Thank you

    submitted by /u/mpapapa
    [link] [comments]

    String array comparison problem

    Posted: 17 Feb 2020 03:52 PM PST

    I'm trying to compare the contents of a string array with another string. In the code below, I am checking whether the contents are "int" or something else. The issue I'm having is that there is no output. If the string array was [int, a, =, b, *, c], the output should be "keyword: int". However, I'm getting no output with the current if statement.

     public static void main(String[] args) throws FileNotFoundException { try { //User input file directory Scanner userInput = new Scanner(System.in); System.out.print("File directory: "); // C:/Users/Hamido/Documents/test.txt String fileDirectory = userInput.next(); userInput.close(); //import file located at given directory File file = new File(fileDirectory); //Read file located at given directory Scanner fileScan = new Scanner(file); //Lexical analysis of file while(fileScan.hasNextLine()) { String[] line = fileScan.nextLine().split("(?==)|(?<==)" //operator -> = + "|(?=\\))|(?<=\\))" //operator -> ) + "|(?=\\()|(?<=\\()" //operator -> ( + "|(?=\\+)|(?<=\\+)" //operator -> + + "|(?=\\-)|(?<=\\-)" //operator -> - + "|(?=\\*)|(?<=\\*)" //operator -> * + "|(?=\\/)|(?<=\\/)" //operator -> / + "|(?=\\\")|(?<=\\\")" //operator -> " + "|(?=\\,)|(?<=\\,)" //operator -> , + "|(?=\\;)" //operator -> ; + "|(?=int)(?=double)(?=String)"); //keywords -> int, double, and String //System.out.println(Arrays.toString(line)); for (int i = 0; i < line.length; i++) { if(line[i] == "int") System.out.println("keyword: " + line[i]); } } fileScan.close(); } catch (FileNotFoundException fnfe){ System.out.println("File not found"); } } } 
    submitted by /u/Suspicious_Phrase
    [link] [comments]

    Reprogramming a console, hard level.

    Posted: 17 Feb 2020 02:07 PM PST

    Hey I just had question and wasn't sure it is even possible.

    I guess I'm just curious is that, is there a way in theory to recreate a Nintendo 64 console, and allow for when someone puts in the cart to add different shaders and stuff, like emulator but on hard level.

    Not a clone console, but more so a console that reads the data of the cart, and in the hardware, manipulates that data and adds layers of shaders and mods, like emulators do.

    I know most clone consoles just check your game and add a rom then emulate it. I'm more looking for a way to do what emulators do. However with no emulation. if that makes sense.

    I'm more here for knowledge, and just curious if it can be done. I think the Nintendo 64 is good example since today it really does not hold up graphically.

    submitted by /u/myunholysins
    [link] [comments]

    Requesting help with SQL. Variation on what a full outer join does

    Posted: 17 Feb 2020 01:16 PM PST

    Table A id | symbolA 1 m 2 n 4 o Table B id | symbolB 2 p 3 q 5 r 

    I'd like the following result:

    id | symbolA | symbolB 2 n p 1 m 4 o 3 q 5 r 
    submitted by /u/SWEWorkAccount
    [link] [comments]

    Variable Explorer for Cling?

    Posted: 17 Feb 2020 12:33 PM PST

    Is there a variable explorer for Cling, like in Matlab or %whos for ipython?

    submitted by /u/xeroblaze0
    [link] [comments]

    Reader not reading Intagers as strings.

    Posted: 17 Feb 2020 12:07 PM PST

    I have txt file that is converting each letter of the Bee Movie to a specified number, (First part of the code (Red Scanner)) For the next part of the code I would like the code to take each number and turn it into its respective abriviation of an element from the periodic table.

    The code, as far as I can is not reading the numbers as numbers (Like 1 or 34) to convert them and i have no idea why.

    Any help would be great. Thanks :)

    **Code**

    package thebeemovie;

    import java.util.Random;

    import java.util.Scanner;

    import java.io.*;

    public class TheBeemovie {

    public static void main(String[] args)throws IOException {

    Scanner Red = new Scanner(new File("D:\\BeeMovie.txt"));

    PrintWriter Adventure = new PrintWriter(new File("D:\\UBetterHeckinWork.txt"));

    int number = 0;

    while (Red.hasNext()){

    String letter;

    letter = Red.nextLine();

    int i = 0;

    while(i < letter.length()){

    char e = (letter.charAt(i));

    i++;

    switch(e){

    case 'a': Adventure.print("1 ");

    break;

    case 'b': Adventure.print("2 ");

    break;

    case 'c': Adventure.print("3 ");

    break;

    case 'd': Adventure.print("4 ");

    break;

    case 'e': Adventure.print("5 " );

    break;

    case 'f': Adventure.print("6 ");

    break;

    case 'g': Adventure.print("7 ");

    break;

    case 'h': Adventure.print("8 ");

    break;

    case 'i': Adventure.print("9 " );

    break;

    case 'j': Adventure.print("10 ");

    break;

    case 'k': Adventure.print("11 ");

    break;

    case 'l': Adventure.print("12 ");

    break;

    case 'm': Adventure.print("13 ");

    break;

    case 'n': Adventure.print("14 ");

    break;

    case 'o': Adventure.print("15 ");

    break;

    case 'p': Adventure.print("16 ");

    break;

    case 'q': Adventure.print("17 ");

    break;

    case 'r': Adventure.print("18 ");

    break;

    case 's': Adventure.print("19 ");

    break;

    case 't': Adventure.print("20 ");

    break;

    case 'u': Adventure.print("21 ");

    break;

    case 'v': Adventure.print("22 ");

    break;

    case 'w': Adventure.print("23 ");

    break;

    case 'x': Adventure.print("24 ");

    break;

    case 'y': Adventure.print("25 ");

    break;

    case 'z': Adventure.print("26 ");

    break;

    case 'A': Adventure.print("27 ");

    break;

    case 'B': Adventure.print("28 ");

    break;

    case 'C': Adventure.print("29 ");

    break;

    case 'D': Adventure.print("30 ");

    break;

    case 'E': Adventure.print("31 ");

    break;

    case 'F': Adventure.print("32 ");

    break;

    case 'G': Adventure.print("33 ");

    break;

    case 'H': Adventure.print("34 ");

    break;

    case 'I': Adventure.print("35 ");

    break;

    case 'J': Adventure.print("36 ");

    break;

    case 'K': Adventure.print("37 ");

    break;

    case 'L': Adventure.print("38 ");

    break;

    case 'M': Adventure.print("39 ");

    break;

    case 'N': Adventure.print("40 ");

    break;

    case 'O': Adventure.print("41 ");

    break;

    case 'P': Adventure.print("42 " );

    break;

    case 'Q': Adventure.print("43 ");

    break;

    case 'R': Adventure.print("44 ");

    break;

    case 'S': Adventure.print("45 ");

    break;

    case 'T': Adventure.print("46 ");

    break;

    case 'U': Adventure.print("47 ");

    break;

    case 'V': Adventure.print("48 ");

    break;

    case 'W': Adventure.print("49 ");

    break;

    case 'X': Adventure.print("50 ");

    break;

    case 'Y': Adventure.print("51 " );

    break;

    case 'Z': Adventure.print("52 ");

    break;

    case ' ': Adventure.print("53 ");

    break;

    default: Adventure.print("54 ");

    break;

    } } }

    System.out.println("Working");

    Scanner Blue = new Scanner(new File("D:\\UBetterHeckinWork.txt"));

    PrintWriter tp = new PrintWriter(new File("D:\\AnotherOneBitesTheJava.txt"));

    String number1;

    String number2;

    //number1 = Blue.next();

    //number2 = Blue.next();

    while(Blue.hasNext()){

    String NotLetter = Blue.nextLine();

    String[] numberString = NotLetter.split("\\s+");

    System.out.println("Workinging");

    for(int j = 0;j < numberString.length;j++){

    int numbers[] = {j}; //dclaring array

    numbers = new int[j];

    for( j=0; j<numbers.length; j++)

    for(j=1; j<numbers.length; j++)

    numbers[j] = Integer.parseInt(numberString[j]);

    /** switch(j){

    case 0: NotLetter = number1;

    break;

    case 1: NotLetter = number2;

    break;

    } **/

    // while(Blue.hasNext()){

    // String nice;

    // nice = Blue.nextLine();

    // int i = 0;

    //while(i < nice.length()){

    // nice = (tp.substring(i));

    // i++;

    switch(NotLetter.substring(j)){

    case "1": tp.print("H ");

    break;

    case "2":tp.print("He ");

    break;

    case "3":tp.print("Li " );

    break;

    case "4":tp.print("Be ");

    break;

    case "5":tp.print("B ");

    break;

    case "6":tp.print("C ");

    break;

    case "7":tp.print("N ");

    break;

    case "8": tp.print("O ");

    break;

    case "9":tp.print("F ");

    break;

    case "10":tp.print("Ne ");

    break;

    case "11":tp.print("Na ");

    break;

    case "12":tp.print("Mg ");

    break;

    case "13":tp.print("Al ");

    break;

    case "14":tp.print("Si ");

    break;

    case "15":tp.print("P ");

    break;

    case "16":tp.print("S ");

    break;

    case "17":tp.print("Cl ");

    break;

    case "18":tp.print("Ar ");

    break;

    case "19":tp.print("K ");

    break;

    case "20":tp.print("Ca ");

    break;

    case "21":tp.print("Sc ");

    break;

    case "22":tp.print("Ti ");

    break;

    case "23":tp.print("V ");

    break;

    case "24":tp.print("Cr ");

    break;

    case "25":tp.print("Mn ");

    break;

    case "26": tp.print("Fe ");

    break;

    case "27":tp.print("Co ");

    break;

    case "28":tp.print("Ni ");

    break;

    case "29":tp.print("Cu ");

    break;

    case "30":tp.print("Zn ");

    break;

    case "31":tp.print("Ga ");

    break;

    case "32":tp.print("Ge ");

    break;

    case "33": tp.print("As");

    break;

    case "34":tp.print("Se");

    break;

    case "35":tp.print("Br");

    break;

    case "36":tp.print("Kr");

    break;

    case "37":tp.print("Rb");

    break;

    case "38":tp.print("Sr");

    break;

    case "39":tp.print("Y");

    break;

    case "40":tp.print("Zr");

    break;

    case "41":tp.print("Nb");

    break;

    case "42":tp.print("Mo");

    break;

    case "43":tp.print("Tc");

    break;

    case "44":tp.print("Ru");

    break;

    case "45":tp.print("Rh");

    break;

    case "46":tp.print("Pd");

    break;

    case "47":tp.print("Ag");

    break;

    case "48":tp.print("Cd");

    break;

    case "49":tp.print("In");

    break;

    case "50":tp.print("Sn");

    break;

    case "51":tp.print("Sb");

    break;

    case "52":tp.print("Te");

    break;

    case "53":tp.print("53");

    case " ":tp.print(" ");

    case "54":tp.print("Xe");

    break;

    default:tp.print("PleaseWorkOnTheProject:)");

    } } }

    System.out.println("The operation is compleation. ");

    tp.close();

    Adventure.close();

    }}

    submitted by /u/DyAstronaght
    [link] [comments]

    Sorting methods

    Posted: 17 Feb 2020 11:26 AM PST

    I have 2 unknown sorting algorithms, sort1 and sort2. I know that one is mergesort and one is heapsort. I have made a plot of the running time for various sizes of arrays (ordered array, reverse ordered and random) and I cant tell the difference. But I know that mergesort it stable and that heapsort isn't. How do I figure out which method is unstable?

    submitted by /u/thepinkpanda1
    [link] [comments]

    Creating a check-in signaling system that would write data

    Posted: 17 Feb 2020 09:49 AM PST

    I understand that this is a very general question, but I'm not sure what to look for research, nor how to start. English is not my native language, so bear with me.

    • What is my goal?

    I have a device A. I want to set up another signaling device S that activates whenever electric current passes through it. I can set up cable management for device S, but my question is, what type of device should I use? I need something cheap because the only purpose for device S would be to send a signal whenever it receives power and then send a signal right before it has no longer power. So, two signals: One when it's activated and another one when it's deactivated. That's it. But, the ability to send that signal should be wireless.

    So, device S activates when electricity passes through it and then sends a signal to device A via wireless. The distance between these devices is 10 meters at max. After that, I need device A to send parameters boolean (true whenever the device is activated and then false when it's deactivated or something like that) and the timestamp to a PC, again via wireless, which is another 10 meters away from device A. Note that the timestamp is not actually mandatory. I can set it up via the PC.

    Device A would be the controlling device that would send info to the PC. The possibility of multiple devices S should be considered. Meaning that in the future there may be more than one and they should have an ID and other stuff to sent back to.

    I've made a scheme so you get the idea.

    So, what devices should I look for A and S? I think I'll figure out the programming part for those devices on my own, but I need them to be compatible and stuff.

    submitted by /u/hurt_spaghet
    [link] [comments]

    Is there a bigger expectation for software students today when making the final year project much harder today as opposed to 20 years ago with the internet resource at our disposal?

    Posted: 17 Feb 2020 03:18 AM PST

    Android: Split text to a new line whenever a comma appears

    Posted: 17 Feb 2020 08:33 AM PST

    I've an edittext that is populated with a string based on a previous selection. Is there a way to fill the edittext but whenever a comma appears but the next piece on text onto the next line

    submitted by /u/Demerson96
    [link] [comments]

    Where to begin for my project?

    Posted: 17 Feb 2020 08:21 AM PST

    hello reddit.

    I have little knowledge of how to code. i took a couple of classes in college but its been a few years. i am trying to make an app that can record elevation/height traveled and turn it into a score. where should i start? how can i make something like this? and recommendations would be very appreciated. just looking to be pointed in the right direction.

    submitted by /u/Ace8o_o8
    [link] [comments]

    r Programming- Monopoly Code

    Posted: 17 Feb 2020 11:51 AM PST

    Hi, I am trying to running this on R Version 3.6.2

    Output of rolling_double is not coming as expected. Can you please see and suggest changes

    Thanks in advance!

    Monopoly Board game simulationYou will create a simulation of the classic board game, Monopoly. The goal is to find out which spaces on the board get landed on the most.

    You will not simulate the entire game. You will simulate only the movement of pieces, and will keep track of which squares the pieces land on. If you have never played Monopoly before, I recommend watching a few videos on the topic. https://www.youtube.com/watch?v=4Hfe97Q5kuI

    You can also familiarize yourself with the game board. (Taken from Amazon's product page.)

    http://ecx.images-amazon.com/images/I/81oC5pYhh2L._SL1500_.jpg

    Official rules http://www.hasbro.com/common/instruct/00009.pdf

    Rules for movement

    The Monopoly Board is effectively a circle with 40 spaces on which a player can land. Players move from space to space around the board in a circle (square).

    The number of spaces a player moves is determined by the roll of 2 dice. Most often, the player will roll the dice, land on a space, and end his turn there. If this were the entire game, the spaces would have a uniform distribution after many turns.

    There are, however, several exceptions which provide the primary source of variation in space landing

    Go to Jail

    One space, "Go to Jail" sends players directly to jail (there is a jail space on the board). This space never counts as having been 'landed upon.' As soon as the player 'lands' here, he is immediately sent to jail, and the jail space gets counted as landed upon. This is the only space on the game board that moves a player's piece. The count of how often this space is landed on will always be 0.

    Rolling Doubles

    If a player rolls doubles (two of the same number), the player moves his piece, and then gets to roll the dice again for another move. However, if a player rolls doubles three times in a row, he is sent directly to jail. (The third space that the player would have 'landed on' does not count, but the jail space gets counted as landed on.)

    Card Decks: Chance and Community Chest

    A player can land on a "Chance" or "Community Chest" space. When a player lands on these spaces, he draws a card from the respective deck and follows its instructions. The instructions will sometimes give money to or take money from the player with no change in the player's position on the board. Other times, the card will instruct the player to move to another space on the board. The list of cards that can be drawn from each deck is provided below.

    There are nine cards in the Chance deck that move the player's token. There are two cards in the Community Chest deck that move the player's token. All other cards do not move the player's token. For the sake of this simulation, you only need to program actions for the cards that move the tokens. There is no need to do anything for 'get out of jail' or any of the other cards.

    A card may say 'move to the nearest railroad' or 'move to the nearest utility' or even 'go to property xxx'. In these cases, the player always moves forward. So if a player is on 'Oriental Avenue,' the nearest railroad is 'Pennsylvania Railroad' and NOT 'Reading Railroad.'

    The Chance and Community Chest get counted as landed on when the player lands on the Chance or Community Chest space. The player may also generate another count if the card moves the player to another space on the board. In those cases, a tally is counted for the Chance/Community Chest space, the token is moved, and then a tally is counted for the space where the player ends his turn.

    Jail

    Jail is the most complicated aspect of this simulation.

    If a player lands on space 11 (Jail), he is not in Jail. He is 'just visiting' jail. He generates a tally for landing on jail, and his play continues on as normal.

    A player can be sent to jail in several ways: he rolls doubles three times in a row; he lands on the "go to jail" space; he draws a card that sends hims to jail.

    As soon as the player is sent to jail, his token moves to jail (space 11), he generates a count for landing on jail, and his turn ends immediately.

    On the next turn, the player begins in jail and the player will roll the dice. If he rolls doubles on the dice, he gets out of jail and moves the number of spaces the dice show. However, even though he rolled doubles, he does NOT roll again. He takes his move out of jail and his turn ends. If he does not roll doubles, he stays in jail.

    A player cannot stay in jail for more than three turns. On the third turn he begins in jail, he rolls the dice and moves the number of spaces the dice show no matter what.

    Play then continues as normal.

    For this simulation, each time a player ends his turn in Jail, a tally will be counted as having been 'landed upon.'

    There are more rules on jail that include paying a fee to get out early, or using a get out of jail free card. We will not implement those rules. We will simply simulate a 'long stay' strategy for Jail. This means that the player will never pay the fee to get out jail early. He will roll the dice and only leave jail if he gets doubles or it is his third turn in jail.

    The Assignment

    Your assignment is to implement the rules of Monopoly movement.

    Part 1

    You will first demonstrate that you have codes the rules by showing the output of the first 20 turns using the preset dice. Your output for this section should match the published results almost exactly. Turns 1-17 should be exactly the same. On turn 18, you might draw a different random chance card from me. This is allowed.

    The output should be very verbose. It should announce the player roles, where the player moves, what spaces get tallies, if they rolled doubles, etc. Try to match my published output as much as possible.

    Part 2

    The next part is to run 1,000 simulations of a two-player game that lasts 150 turns. This is a total of over 3 hundred thousand tosses of the dice - 1000 games x 150 turns x 2 players + additional rolls if the player gets doubles.

    Your task is to keep track of where the players land. We ultimately want to build a distribution showing which spaces are most likely to be landed upon. Advance the tokens around the board according to the rules. Keep in mind the special situations involving the cards, jail, and rolling doubles. After 150 turns, reset the game and start over. Simulate 1000 games.

    Your final output will be two tables of the spaces on the board and their frequencies. Each table will show the space name, how many times the space was landed upon, and the relative frequency of landing on that space.

    The first table is arranged in descending order of frequency of landing. (Jail should be #1, Go to jail should be last.)

    The second table is arrange in the order of the spaces on the board. (Go will be #1, Boardwalk will be last.)

    You do not have to simulate or track money at all in this simulation.

    For your convenience, I have created the necessary data frames for the game board, and the two decks of cards.

    You can 'hard code' the functions that handle the decks. In other words, you can write something along the lines of

    ## for chance deck ... if(carddrawn == 1) code changes player position to space 1 # advance to go if(carddrawn == 2) code changes player position to space 25 # advance to Illinois avenue # etc. ... 

    Tips

    At first blush, the task may seem overwhelming.

    • Break the task into smaller manageable parts.
    • Start with a simulation that moves pieces around the board and keeps track of where they land. (I've done this part for you in my example code.)
    • Then add complexity one part at a time.
    • Add something so landing on "Go to jail" sends the player to jail.
    • Add functions for the Chance and Community Chest decks. Keep in mind that some cards have no effect on player movement, while other cards do.
    • Add something to allow players to move again after doubles.
    • Finally implement the Jail. You'll need to keep track of whether the player is actually in jail or not, how many turns the player has been in jail, and the rules for getting out.

    steps

    1. Do NOT print the verbose version for all of 1000 games.
    2. We will not run your code. The simulation will take time to run, and we do not have the luxury of running the entire simulation for all students.
    3. We will check the output of the 20 preset turns. The output for the first 17 turns should match exactly.
    • Turn 1-7 (2 pts each x 7 = 14)
    • Turn 8 (6 pts)
    • Turn 9-15 (3 pts each x 7 = 21)
    • Turn 16-17 (4 pts each = 8)
    • On turn 18 you draw a chance card (4 pts),

      • tally a spot at chance (3 pts),
      • and follow the actions on the chance card (4 pts)

    That is 60 points. Turn 19 and 20 are not graded.

    1. We will check the final output of counts in the results tables.

    Things we are looking for in the results tables (5 points each):

    • Jail should be the most frequent space landed on.
    • Jail should have a frequency between 10 and 13%.
    • Illinois Avenue should be landed on very frequently. It should be in the top 5.
    • Go should be landed on very frequently. It should be in the top 5.
    • Mediterranean Ave should be very infrequent. Bottom 8.
    • Baltic should be very infrequent. Bottom 10.
    • Park Place should be very infrequent. Bottom 10.
    • Go to jail should be landed on 0 times.
    1. If your 20 turn output and table output matches what we are looking for, you will get full credit.
    2. If your outputs do not match, We will skim your code to see if certain functions or chunks of code exist to see if you can recover some (up to half) of the points you lost.

    We will check for the following sections. Because your code will be quite long, it is VERY IMPORTANT you CLEARLY mark these sections for the grader if you want to recover points.

    a. drawing chance card b. drawing community chest card c. landing on "go to jail" d. roll again for rolling doubles e. going to jail for rolling three doubles f. jail functionality

    # dice -------------------------------------------------------------------- dice <- function()... # drawing chance card ----------------------------------------------------- etc. etc. gameboard <- data.frame(space = 1:40, title = c("Go" , "Mediterranean Avenue" , "Community Chest" , "Baltic Avenue" , "Income Tax" , "Reading Railroad" , "Oriental Avenue" , "Chance" , "Vermont Avenue" , "Connecticut Avenue" , "Jail" , "St. Charles Place" , "Electric Company" , "States Avenue" , "Virginia Avenue" , "Pennsylvania Railroad" , "St. James Place" , "Community Chest" , "Tennessee Avenue" , "New York Avenue" , "Free Parking" , "Kentucky Avenue" , "Chance" , "Indiana Avenue" , "Illinois Avenue" , "B & O Railroad" , "Atlantic Avenue" , "Ventnor Avenue" , "Water Works" , "Marvin Gardens" , "Go to jail" , "Pacific Avenue" , "North Carolina Avenue" , "Community Chest" , "Pennsylvania Avenue" , "Short Line Railroad" , "Chance" , "Park Place" , "Luxury Tax" , "Boardwalk"), stringsAsFactors = FALSE) chancedeck <- data.frame(index = 1:15, card = c("Advance to Go" , "Advance to Illinois Ave." , "Advance to St. Charles Place" , "Advance token to nearest Utility" , "Advance token to the nearest Railroad" , "Take a ride on the Reading Railroad" , "Take a walk on the Boardwalk" , "Go to Jail" , "Go Back 3 Spaces" , "Bank pays you dividend of $50" , "Get out of Jail Free" , "Make general repairs on all your property" , "Pay poor tax of $15" , "You have been elected Chairman of the Board" , "Your building loan matures"), stringsAsFactors = FALSE) communitydeck <- data.frame(index = 1:16, card = c("Advance to Go" , "Go to Jail" , "Bank error in your favor. Collect $200" , "Doctor's fees Pay $50" , "From sale of stock you get $45" , "Get Out of Jail Free" , "Grand Opera Night Opening" , "Xmas Fund matures" , "Income tax refund" , "Life insurance matures. Collect $100" , "Pay hospital fees of $100" , "Pay school tax of $150" , "Receive for services $25" , "You are assessed for street repairs" , "You have won second prize in a beauty contest" , "You inherit $100"), stringsAsFactors = FALSE) ### To get you started, here is a simple function to roll two dice. # Dice -------------------------------------------------------------------- random_dice <- function(verbose=FALSE){ faces <- sample(1:6, 2, replace=TRUE) if(faces[1] == faces[2]) doubles = TRUE else doubles = FALSE movement = sum(faces) if(verbose) cat("Rolled:", faces[1], faces[2], "\n") return(list(faces=faces, doubles=doubles, movement=movement)) } # Manual Dice ------------------------------------------------------------- # this Reference Class allows you to create a set of manual dice manual_dice = setRefClass("manual_dice", fields = list( rolls = "numeric", pos = "numeric", verbose = "logical" ), methods = list( roll = function() { faces = rolls[pos + seq_len(2)] pos <<- pos + 2 if(faces[1] == faces[2]) doubles = TRUE else doubles = FALSE movement = sum(faces) if(verbose) cat("Rolled:", faces[1], faces[2], "\n") return(list(faces=faces, doubles=doubles, movement=movement)) } ) ) # preset_rolls is an instance of the manual_dice reference class preset_rolls <- manual_dice$new(rolls = c(6,4, 5,3, 3,5, 4,4, 4,4, 2,2, 4,3, 4,4, 1,4, 3,4, 1,2, 3,6, 5,4, 5,5, 1,2, 5,4, 3,3, 6,1, 1,1, 2,3, 5,5, 5,4, 4,1, 2,2, 2,4), pos = 0, verbose = TRUE) # preset_dice is a function that simply calls the roll() method from this preset_rolls object preset_dice <- function() preset_rolls$roll() # I highly recommend the use of a reference class to keep track of each player. # I've created a *very basic* reference class with a pair of methods to get you started. # This starting reference class has only one field in it and only two methods # You'll definitely want to add more fields and methods, but I'll let you figure out and decide what those are. # Player Reference Class -------------------------------------------------- # a **very basic** reference class for our players player <- setRefClass("player", fields = list( pos = "numeric", # position on the board verbose = "logical", # option to have it print all the info double_count = "numeric", jail_count ="numeric", jail= "logical" ), methods = list( move_fwd = function(x) { if(verbose) cat("Player at:", pos) if(verbose) cat(" Player moves:", x) pos <<- pos + x if (pos == 31) { if(verbose) cat("\nPlayer now at:",pos,"\nGoing to Jail\n") jail <<- TRUE pos <<- 11 } if(pos > 40) pos <<- pos - 40 if(verbose) cat(" \nPlayer now at:", pos,",",gameboard$title[pos],".","\n") }, go_2_space_n = function(n){ if(verbose) cat("Player at:", pos,".") pos <<- n if(verbose) cat(" Player now at:", pos,".\n") }, is_jail_true = function(){ jail<<-TRUE }, ### JAIL FUNCTIONALITY FOR ROLLING DOUBLES AND GOING TO JAIL FOR THREE ### rolling_doubles = function(player,tracking){ double_count <<- 0 while(double_count<=3){ double_count <<- double_count+1 if(double_count<3){ if(verbose) cat("Double count is now:",double_count,"\n") if(double_count+1==3){player$move_fwd(roll$movement)} tracking$increase_count(player$pos) if(verbose) cat("Rolled doubles, take another turn.","\n") jail<<- FALSE } else if(double_count==3) { if(verbose) cat("Double count now is:",double_count,"\n") player$go_2_space_n(11) tracking$increase_count(player$pos) jail <<-TRUE break } roll <- dice() if (roll$doubles == FALSE) { player$move_fwd(roll$movement) tracking$increase_count(player$pos) jail <<- FALSE break } } }, ### PLAYER IN JAIL, COUNTING TURNS IN JAIL,ROLLING TO GET OUT ### player_in_jail = function(player,tracking){ jail <<- TRUE jail_count <<- jail_count + 1 if(verbose) cat("\nGoing to jail") if(verbose) cat("\nThe turn in Jail is:",jail_count,"\n") while (jail == TRUE) { roll_in_jail <- dice() if(jail_count==3){ if(verbose) cat("Player out of jail!","\n") player$move_fwd(roll_in_jail$movement) tracking$increase_count(player$pos) jail<<-FALSE jail_count <<- 0 } else{ if(roll_in_jail$doubles==TRUE){ if(verbose) cat("Player out of jail!","\n") player$move_fwd(roll_in_jail$movement) tracking$increase_count(player$pos) jail<<-FALSE jail_count <<- 0 } else{ if(verbose) cat("Player staying in Jail. \n") tracking$increase_count(player$pos) break } ### LANDING ON 31 GO TO JAIL ### if(pos==31){ go_2_space_n(11) tracking$increase_count(player$pos) jail<<-TRUE } } } } ) ) ### COMMUNITY CHEST CARDS ### community_chest_draw <- function(player,tracking){ community_card <- sample(1:16, 1, replace = TRUE) if (community_card == 1) { player$go_2_space_n(1) tracking$increase_count(player$pos) if(player$verbose) cat("Community Chest Card", community_card,communitydeck$card[1]) } if (community_card == 2) { if(player$verbose) cat("Community Chest Card", community_card,communitydeck$card[2]) player$is_jail_true() player$go_2_space_n(11) tracking$increase_count(11) } if (community_card %in% 3:16) { if(player$verbose) cat("Tally at", player$pos, ":", gameboard$title[player$pos], "\n") tracking$increase_count(player$pos) if(player$verbose) cat("Draw Community Chest Card", community_card, "\n") } } ### CHANCE CARD DRAW ### chance_card_draw <- function(player,tracking){ chance_card <- sample(1:15, 1, replace = TRUE) if(player$verbose) cat("Draw Chance Card\n") if (chance_card == 1) { player$go_2_space_n(1) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[1]) } if (chance_card == 2) { player$go_2_space_n(25) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[2]) } if (chance_card == 3) { player$go_2_space_n(12) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[3]) } if (chance_card == 4) { if (player$pos %in% 14:28) { player$go_2_space_n(29) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[4]) } else { player$go_2_space_n(13) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[4]) } } if (chance_card == 5) { if (player$pos == 8) { player$go_2_space_n(16) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card,chancedeck$card[5]) } else if (player$pos == 23) { player$go_2_space_n(26) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card,chancedeck$card[5]) } else if(player$pos==37) { player$go_2_space_n(6) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[5]) } } if (chance_card == 6) { player$go_2_space_n(6) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[6]) } if (chance_card == 7) { player$go_2_space_n(40) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[7]) } if (chance_card == 8) { if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[8], "\n") player$is_jail_true() player$go_2_space_n(11) tracking$increase_count(11) } if (chance_card == 9) { if (player$pos == 8 ) { player$go_2_space_n(5) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card,chancedeck$card[9]) } if (player$pos == 23 ) { player$go_2_space_n(20) tracking$increase_count(player$pos) if(player$verbose) cat("Chance Card", chance_card,chancedeck$card[9]) } if (player$pos == 37 ) { player$go_2_space_n(34) if(player$verbose) cat("Chance Card", chance_card, chancedeck$card[9]) community_chest_draw(player,tracking) } } if (chance_card %in% 10:15) { tracking$increase_count(player$pos) } } # Space Tracking Reference Class ------------------------------------------ # a reference class to keep track of where people landed # I didn't need to make any modifications to this for my final code tracking <- setRefClass("tracking", fields = list( tally = "numeric", verbose = "logical" ), methods = list( increase_count = function(n){ tally[n] <<- tally[n] + 1 if(verbose) cat("Tally at ",n,": ", gameboard$title[n],"\n",sep="") } ) ) # Taking a turn ----------------------------------------------------------- # take_turn accepts a player object and a tracking object. # The take_turn code executes methods in the player object that will # alter variables in the player object # take_turn also executes methods in the tracking object that modifies its values taketurn <- function(player, tracking){ if(player$jail==FALSE){ roll <<- dice() player$move_fwd(roll$movement) if(player$pos == 3 | player$pos == 18 | player$pos == 34){ community_chest_draw(player,tracking) } if (player$pos == 8 | player$pos == 23 | player$pos == 37){ chance_card_draw(player,tracking) } ### LANDING ON GO TO JAIL ### if (player$pos == 31) { player$go_2_space_n(11) tracking$increase_count(11) player$is_jail_true() } if (roll$doubles == TRUE & player$jail==FALSE) { player$rolling_doubles(player, tracking) } if(roll$doubles == FALSE & (player$pos != 3 & player$pos != 18 & player$pos != 34 & player$pos != 8 & player$pos != 23 & player$pos != 37)) { tracking$increase_count(player$pos) } } else{ if(player$jail==TRUE){ player$player_in_jail(player,tracking) } } } # Part 1: 20 turns of the Manual Dice ----------------------------------------------- dice <- function() preset_dice() # we alias preset_dice to dice() # by renaming dice(), we can use the same take_turn function we created and use a # different method for generating dice rolls set.seed(2) space_tracking <- tracking$new(tally = rep(0,40), verbose = TRUE) # new blank tracking object player1 <- player$new(pos = 1, verbose = TRUE,jail_count =0, jail= FALSE, double_count=0) # new player set to verbose for(i in 1:20){ # 20 turns cat("\n## Turn", i,"\n") taketurn(player1, space_tracking) } # Part 2: Running a simulation of 1000 games ----------------------------------------- set.seed(1) dice <- function() random_dice(verbose = FALSE) # we alias random_dice(verbose = FALSE) to dice() space_tracking <- tracking$new(tally = rep(0,40), verbose = FALSE) # new blank tracking object start_time <- Sys.time() for(i in 1:1000){ # simulate 1000 games if(i %% 100 == 0) cat("#### NEW GAME",i,"##### \n") player1 <- player$new(pos = 1, verbose = FALSE,jail_count =0, jail= FALSE, double_count=0) # new players for each game player2 <- player$new(pos = 1, verbose = FALSE,jail_count =0, jail= FALSE, double_count=0) for(i in 1:150){ # 150 turns for each game if(player1$verbose) cat("Player 1 turn\n") taketurn(player1, space_tracking) if(player2$verbose) cat("Player 2 turn\n") taketurn(player2, space_tracking) } } end_time <- Sys.time() # the results after 1000 turns. No rules have been implemented yet. Takes about 34 seconds on my computer # after implementing all the rules, it takes about 1 min 40 seconds to run time_dif = end_time - start_time cat('Simulation time: ', time_dif) results <- cbind(gameboard, tally = space_tracking$tally) results <- cbind(results, rel = results$tally/sum(results$tally)) results_arranged <- results[order(results$tally, decreasing = TRUE), ] print(results_arranged) print(results) # pretty uniform sum(results$tally) barplot(results$tally, names.arg = results$title, horiz = FALSE, las = 2) 
    submitted by /u/sjking1880
    [link] [comments]

    What do you do when you draw a blank, and everything seems too hard, and not worth the effort, and you are just sick of all the bs?

    Posted: 17 Feb 2020 06:22 AM PST

    Like, today, I was writing my Electron app, and it was supposed to be a nice relaxed day where I write something I already made a proof of concept of. And then shit started hitting the fan. "this sucks, I gotta put it in a nice function".. .."why tf am I writing this shitty function that I can't follow and only use in one place, I'll just go back to the basics and write it line by line". ..." why tf am I writing it line by line, this doesn't work without a function"... and so in a circle. I was so annoyed, thought about dropping the whole thing because I have been writing this shit on and off for 2 months, and all I have to show for it is the spongebob empty paper meme with just "The" written - 300 lines, and 4 abandoned versions of the project.

    So I said fuck it, and went for a bike ride to let off some steam.

    And then I came home, and it hit me that the best and most logical solution would be to make a good ol' modular agnostic function with 10 parameters that I can use in the two places I need, and I can adjust the parameter values outside if they aren't in a good format. And then in the shower, I wrote the whole thing in my head, and then I wrote it down, and outside of a few classics like missing "document." or a comma or a parantheses, it worked immediately, and perfectly.

    So yea. This shit is too stressful sometimes. I can't imagine how it can be when there is actual money on the line, and not just jerking off code for fun and edification.

    submitted by /u/asssed
    [link] [comments]

    No comments:

    Post a Comment